How to use step method in testing-library-react-hooks

Best JavaScript code snippet using testing-library-react-hooks

steps.py

Source:steps.py Github

copy

Full Screen

...173 @when('I run "{command}"')174 Function: step_i_run_command()175 Location: behave4cmd0/command_steps.py:80176 Run a command as subprocess, collect its output and returncode.177 @step('a file named "{filename}" exists')178 Function: step_file_named_filename_exists()179 Location: behave4cmd0/command_steps.py:305180 Verifies that a file with this filename exists.181 .. code-block:: gherkin182 Given a file named "abc.txt" exists183 When a file named "abc.txt" exists184 ...185 .. note::186 Supports behave dry-run mode.187 """188 name = "steps.doc"189 description = "Shows documentation for step definitions."190 shows_location = True191 shows_function_name = True192 ordered_by_location = True193 doc_prefix = make_indentation(4)194 # -- REPORT SPECIFIC-API:195 def report(self):196 self.report_step_definition_docs()197 self.stream.write("\n")198 def report_step_definition_docs(self):199 step_definitions = []200 for step_type in self.step_types:201 for step_definition in self.step_registry.steps[step_type]:202 # step_definition.step_type = step_type203 assert step_definition.step_type is not None204 step_definitions.append(step_definition)205 if self.ordered_by_location:206 step_definitions = sorted(step_definitions,207 key=attrgetter("location"))208 for step_definition in step_definitions:209 self.write_step_definition(step_definition)210 def write_step_definition(self, step_definition):211 step_definition_text = self.describe_step_definition(step_definition)212 self.stream.write(u"%s\n" % step_definition_text)213 doc = inspect.getdoc(step_definition.func)214 func_name = step_definition.func.__name__215 if self.shows_function_name and func_name not in ("step", "impl"):216 self.stream.write(u" Function: %s()\n" % func_name)217 if self.shows_location:218 self.stream.write(u" Location: %s\n" % step_definition.location)219 if doc:220 doc = doc.strip()221 self.stream.write(indent(doc, self.doc_prefix))222 self.stream.write("\n")223 self.stream.write("\n")224# -----------------------------------------------------------------------------225# CLASS: StepsCatalogFormatter226# -----------------------------------------------------------------------------227class StepsCatalogFormatter(StepsDocFormatter):228 """229 Provides formatter class that shows the documentation of all registered230 step definitions. The primary purpose is to provide help for a test writer.231 In order to ease work for non-programmer testers, the technical details of232 the steps (i.e. function name, source location) are ommited and the233 steps are shown as they would apprear in a feature file (no noisy '@',234 or '(', etc.).235 Also, the output is sorted by step type (Given, When, Then)236 Generic step definitions are listed with all three step types.237 EXAMPLE:238 $ behave --dry-run -f steps.catalog features/239 Given a file named "{filename}" with240 Creates a textual file with the content provided as docstring.241 When I run "{command}"242 Run a command as subprocess, collect its output and returncode.243 Given a file named "{filename}" exists244 When a file named "{filename}" exists245 Then a file named "{filename}" exists246 Verifies that a file with this filename exists.247 .. code-block:: gherkin248 Given a file named "abc.txt" exists249 When a file named "abc.txt" exists250 ...251 .. note::252 Supports behave dry-run mode.253 """254 name = "steps.catalog"255 description = "Shows non-technical documentation for step definitions."256 shows_location = False257 shows_function_name = False258 ordered_by_location = False259 doc_prefix = make_indentation(4)260 def describe_step_definition(self, step_definition, step_type=None):261 if not step_type:262 step_type = step_definition.step_type263 assert step_type264 desc = []265 if step_type == 'step':266 for step_type in self.step_types[:-1]:267 text = u"%5s %s" % (step_type.title(), step_definition.pattern)268 desc.append(text)269 else:270 desc.append(u"%s %s" % (step_type.title(), step_definition.pattern))271 return '\n'.join(desc)272# -----------------------------------------------------------------------------273# CLASS: StepsUsageFormatter274# -----------------------------------------------------------------------------275class StepsUsageFormatter(AbstractStepsFormatter):276 """277 Provides formatter class that shows how step definitions are used by steps.278 EXAMPLE:279 $ behave --dry-run -f steps.usage features/280 ...281 .. note::282 Supports behave dry-run mode.283 """284 name = "steps.usage"285 description = "Shows how step definitions are used by steps."286 doc_prefix = make_indentation(4)287 min_location_column = 40288 def __init__(self, stream_opener, config):289 super(StepsUsageFormatter, self).__init__(stream_opener, config)290 self.step_usage_database = {}291 self.undefined_steps = []292 def reset(self):293 super(StepsUsageFormatter, self).reset()294 self.step_usage_database = {}295 self.undefined_steps = []296 # pylint: disable=invalid-name297 def get_step_type_for_step_definition(self, step_definition):298 step_type = step_definition.step_type299 if not step_type:300 # -- DETERMINE STEP-TYPE FROM STEP-REGISTRY:301 assert self.step_registry302 for step_type, values in self.step_registry.steps.items():303 if step_definition in values:304 return step_type305 # -- OTHERWISE:306 step_type = "step"307 return step_type308 # pylint: enable=invalid-name309 def select_unused_step_definitions(self):310 step_definitions = set()311 for step_type, values in self.step_registry.steps.items():312 step_definitions.update(values)313 used_step_definitions = set(self.step_usage_database.keys())314 unused_step_definitions = step_definitions - used_step_definitions315 return unused_step_definitions316 def update_usage_database(self, step_definition, step):317 matching_steps = self.step_usage_database.get(step_definition, None)318 if matching_steps is None:319 assert step_definition.step_type is not None320 matching_steps = self.step_usage_database[step_definition] = []321 # -- AVOID DUPLICATES: From Scenario Outlines322 if not steps_contain(matching_steps, step):323 matching_steps.append(step)324 def update_usage_database_for_step(self, step):325 step_definition = self.step_registry.find_step_definition(step)326 if step_definition:327 self.update_usage_database(step_definition, step)328 # elif step not in self.undefined_steps:329 elif not steps_contain(self.undefined_steps, step):330 # -- AVOID DUPLICATES: From Scenario Outlines331 self.undefined_steps.append(step)332 # pylint: disable=invalid-name333 def update_usage_database_for_feature(self, feature):334 # -- PROCESS BACKGROUND (if exists): Use Background steps only once.335 if feature.background:336 for step in feature.background.steps:337 self.update_usage_database_for_step(step)338 # -- PROCESS SCENARIOS: Without background steps.339 for scenario in feature.walk_scenarios():340 for step in scenario.steps:341 self.update_usage_database_for_step(step)342 # pylint: enable=invalid-name343 # -- FORMATTER API:344 def feature(self, feature):345 super(StepsUsageFormatter, self).feature(feature)346 self.update_usage_database_for_feature(feature)347 # -- REPORT API:348 def report(self):349 self.report_used_step_definitions()350 self.report_unused_step_definitions()351 self.report_undefined_steps()352 self.stream.write("\n")353 # -- REPORT SPECIFIC-API:354 def report_used_step_definitions(self):355 # -- STEP: Used step definitions....

Full Screen

Full Screen

sphinx_steps.py

Source:sphinx_steps.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2"""3Provides a formatter that generates Sphinx-based documentation4of available step definitions (step implementations).5TODO:6 * Post-processor for step docstrings.7 * Solution for requires: table, text8 * i18n keywords9.. seealso::10 http://sphinx-doc.org/11.. note:: REQUIRES docutils12 :mod:`docutils` are needed to generate step-label for step references.13"""14from __future__ import absolute_import, print_function15from operator import attrgetter16import inspect17import os.path18import sys19from behave.formatter.steps import AbstractStepsFormatter20from behave.formatter import sphinx_util21from behave.model import Table22try:23 # -- NEEDED FOR: step-labels (and step-refs)24 from docutils.nodes import fully_normalize_name25 has_docutils = True26except ImportError:27 has_docutils = False28# -----------------------------------------------------------------------------29# HELPER CLASS:30# -----------------------------------------------------------------------------31class StepsModule(object):32 """33 Value object to keep track of step definitions that belong to same module.34 """35 def __init__(self, module_name, step_definitions=None):36 self.module_name = module_name37 self.step_definitions = step_definitions or []38 self._name = None39 self._filename = None40 @property41 def name(self):42 if self._name is None:43 # -- DISCOVER ON DEMAND: From step definitions (module).44 # REQUIRED: To discover complete canonical module name.45 module = self.module46 if module:47 # -- USED-BY: Imported step libraries.48 module_name = self.module.__name__49 else:50 # -- USED-BY: features/steps/*.py (without __init__.py)51 module_name = self.module_name52 self._name = module_name53 return self._name54 @property55 def filename(self):56 if not self._filename:57 if self.step_definitions:58 filename = inspect.getfile(self.step_definitions[0].func)59 self._filename = os.path.relpath(filename)60 return self._filename61 @property62 def module(self):63 if self.step_definitions:64 return inspect.getmodule(self.step_definitions[0].func)65 return sys.modules.get(self.module_name)66 @property67 def module_doc(self):68 module = self.module69 if module:70 return inspect.getdoc(module)71 return None72 def append(self, step_definition):73 self.step_definitions.append(step_definition)74# -----------------------------------------------------------------------------75# CLASS: SphinxStepsDocumentGenerator76# -----------------------------------------------------------------------------77class SphinxStepsDocumentGenerator(object):78 """79 Provides document generator class that generates Sphinx-based80 documentation for step definitions. The primary purpose is to:81 * help the step-library provider/writer82 * simplify self-documentation of step-libraries83 EXAMPLE:84 step_definitions = ... # Collect from step_registry85 doc_generator = SphinxStepsDocumentGenerator(step_definitions, "output")86 doc_generator.write_docs()87 .. seealso:: http://sphinx-doc.org/88 """89 default_step_definition_doc = """\90.. todo::91 Step definition description is missing.92"""93 shows_step_module_info = True94 shows_step_module_overview = True95 make_step_index_entries = True96 make_step_labels = has_docutils97 document_separator = "# -- DOCUMENT-END " + "-" * 6098 step_document_prefix = "step_module."99 step_heading_prefix = "**Step:** "100 def __init__(self, step_definitions, destdir=None, stream=None):101 self.step_definitions = step_definitions102 self.destdir = destdir103 self.stream = stream104 self.document = None105 @property106 def stdout_mode(self):107 """108 Indicates that output towards stdout should be used.109 """110 return self.stream is not None111 @staticmethod112 def describe_step_definition(step_definition, step_type=None):113 if not step_type:114 step_type = step_definition.step_type or "step"115 if step_type == "step":116 step_type_text = "Given/When/Then"117 else:118 step_type_text = step_type.capitalize()119 # -- ESCAPE: Some chars required for ReST documents (like backticks)120 step_text = step_definition.pattern121 if "`" in step_text:122 step_text = step_text.replace("`", r"\`")123 return u"%s %s" % (step_type_text, step_text)124 def ensure_destdir_exists(self):125 assert self.destdir126 if os.path.isfile(self.destdir):127 print("OOPS: remove %s" % self.destdir)128 os.remove(self.destdir)129 if not os.path.exists(self.destdir):130 os.makedirs(self.destdir)131 def ensure_document_is_closed(self):132 if self.document and not self.stdout_mode:133 self.document.close()134 self.document = None135 def discover_step_modules(self):136 step_modules_map = {}137 for step_definition in self.step_definitions:138 assert step_definition.step_type is not None139 step_filename = step_definition.location.filename140 step_module = step_modules_map.get(step_filename, None)141 if not step_module:142 filename = inspect.getfile(step_definition.func)143 module_name = inspect.getmodulename(filename)144 assert module_name, \145 "step_definition: %s" % step_definition.location146 step_module = StepsModule(module_name)147 step_modules_map[step_filename] = step_module148 step_module.append(step_definition)149 step_modules = sorted(step_modules_map.values(), key=attrgetter("name"))150 for module in step_modules:151 step_definitions = sorted(module.step_definitions,152 key=attrgetter("location"))153 module.step_definitions = step_definitions154 return step_modules155 def create_document(self, filename):156 if not (filename.endswith(".rst") or filename.endswith(".txt")):157 filename += ".rst"158 if self.stdout_mode:159 stream = self.stream160 document = sphinx_util.DocumentWriter(stream, should_close=False)161 else:162 self.ensure_destdir_exists()163 filename = os.path.join(self.destdir, filename)164 document = sphinx_util.DocumentWriter.open(filename)165 return document166 def write_docs(self):167 step_modules = self.discover_step_modules()168 self.write_step_module_index(step_modules)169 for step_module in step_modules:170 self.write_step_module(step_module)171 return len(step_modules)172 def write_step_module_index(self, step_modules, filename="index.rst"):173 document = self.create_document(filename)174 document.write(".. _docid.steps:\n\n")175 document.write_heading("Step Definitions")176 document.write("""\177The following step definitions are provided here.178----179""")180 entries = sorted([self.step_document_prefix + module.name181 for module in step_modules])182 document.write_toctree(entries, maxdepth=1)183 document.close()184 if self.stdout_mode:185 sys.stdout.write("\n%s\n" % self.document_separator)186 def write_step_module(self, step_module):187 self.ensure_document_is_closed()188 document_name = self.step_document_prefix + step_module.name189 self.document = self.create_document(document_name)190 self.document.write(".. _docid.steps.%s:\n" % step_module.name)191 self.document.write_heading(step_module.name, index_id=step_module.name)192 if self.shows_step_module_info:193 self.document.write(":Module: %s\n" % step_module.name)194 self.document.write(":Filename: %s\n" % step_module.filename)195 self.document.write("\n")196 if step_module.module_doc:197 module_doc = step_module.module_doc.strip()198 self.document.write("%s\n\n" % module_doc)199 if self.shows_step_module_overview:200 self.document.write_heading("Step Overview", level=1)201 self.write_step_module_overview(step_module.step_definitions)202 self.document.write_heading("Step Definitions", level=1)203 for step_definition in step_module.step_definitions:204 self.write_step_definition(step_definition)205 # -- FINALLY: Clean up resources.206 self.document.close()207 self.document = None208 if self.stdout_mode:209 sys.stdout.write("\n%s\n" % self.document_separator)210 def write_step_module_overview(self, step_definitions):211 assert self.document212 headings = [u"Step Definition", u"Given", u"When", u"Then", u"Step"]213 table = Table(headings)214 step_type_cols = {215 # -- pylint: disable=bad-whitespace216 "given": [u" x", u" ", u" ", u" "],217 "when": [u" ", u" x", u" ", u" "],218 "then": [u" ", u" ", u" x", u" "],219 "step": [u" x", u" x", u" x", u" x"],220 }221 for step_definition in step_definitions:222 row = [self.describe_step_definition(step_definition)]223 row.extend(step_type_cols[step_definition.step_type])224 table.add_row(row)225 self.document.write_table(table)226 @staticmethod227 def make_step_definition_index_id(step):228 if step.step_type == "step":229 index_kinds = ("Given", "When", "Then", "Step")230 else:231 keyword = step.step_type.capitalize()232 index_kinds = (keyword,)233 schema = "single: %s%s; %s %s"234 index_parts = []235 for index_kind in index_kinds:236 keyword = index_kind237 word = " step"238 if index_kind == "Step":239 keyword = "Given/When/Then"240 word = ""241 part = schema % (index_kind, word, keyword, step.pattern)242 index_parts.append(part)243 joiner = "\n "244 return joiner + joiner.join(index_parts)245 def make_step_definition_doc(self, step):246 doc = inspect.getdoc(step.func)247 if not doc:248 doc = self.default_step_definition_doc249 doc = doc.strip()250 return doc251 def write_step_definition(self, step):252 assert self.document253 step_text = self.describe_step_definition(step)254 if step_text.startswith("* "):255 step_text = step_text[2:]256 index_id = None257 if self.make_step_index_entries:258 index_id = self.make_step_definition_index_id(step)259 heading = step_text260 step_label = None261 if self.step_heading_prefix:262 heading = self.step_heading_prefix + step_text263 if has_docutils and self.make_step_labels:264 # -- ADD STEP-LABEL (supports: step-refs by name)265 # EXAMPLE: See also :ref:`When my step does "{something}"`.266 step_label = fully_normalize_name(step_text)267 # SKIP-HERE: self.document.write(".. _%s:\n\n" % step_label)268 self.document.write_heading(heading, level=2, index_id=index_id,269 label=step_label)270 step_definition_doc = self.make_step_definition_doc(step)271 self.document.write("%s\n" % step_definition_doc)272 self.document.write("\n")273# -----------------------------------------------------------------------------274# CLASS: SphinxStepsFormatter275# -----------------------------------------------------------------------------276class SphinxStepsFormatter(AbstractStepsFormatter):277 """278 Provides formatter class that generates Sphinx-based documentation279 for all registered step definitions. The primary purpose is to:280 * help the step-library provider/writer281 * simplify self-documentation of step-libraries282 .. note::283 Supports dry-run mode.284 Supports destination directory mode to write multiple documents.285 """286 name = "sphinx.steps"287 description = "Generate sphinx-based documentation for step definitions."288 doc_generator_class = SphinxStepsDocumentGenerator289 def __init__(self, stream_opener, config):290 super(SphinxStepsFormatter, self).__init__(stream_opener, config)291 self.destdir = stream_opener.name292 @property293 def step_definitions(self):294 """Derive step definitions from step-registry."""295 steps = []296 for step_type, step_definitions in self.step_registry.steps.items():297 for step in step_definitions:298 step.step_type = step_type299 steps.append(step)300 return steps301 # -- FORMATTER-API:302 def close(self):303 """Called at end of test run."""304 if not self.step_registry:305 self.discover_step_definitions()306 self.report()307 # -- SPECIFIC-API:308 def create_document_generator(self):309 generator_class = self.doc_generator_class310 if self.stdout_mode:311 return generator_class(self.step_definitions, stream=self.stream)312 else:313 return generator_class(self.step_definitions, destdir=self.destdir)314 def report(self):315 document_generator = self.create_document_generator()316 document_counts = document_generator.write_docs()317 if not self.stdout_mode:318 msg = "%s: Written %s document(s) into directory '%s'.\n"...

Full Screen

Full Screen

_stepimport.py

Source:_stepimport.py Github

copy

Full Screen

1# -*- coding: UTF-8 -*-2"""3This module provides low-level helper functionality during step imports.4.. warn:: Do not use directly5 It should not be used directly except in behave Runner classes6 that need to provide the correct context (step_registry, matchers, etc.)7 instead of using the global module specific variables.8"""9from __future__ import absolute_import10from contextlib import contextmanager11from threading import Lock12from types import ModuleType13import os.path14import sys15from behave import step_registry as _step_registry16# from behave import matchers as _matchers17import six18# -----------------------------------------------------------------------------19# UTILITY FUNCTIONS:20# -----------------------------------------------------------------------------21def setup_api_with_step_decorators(module, step_registry):22 _step_registry.setup_step_decorators(module, step_registry)23def setup_api_with_matcher_functions(module, matcher_factory):24 module.use_step_matcher = matcher_factory.use_step_matcher25 module.step_matcher = matcher_factory.use_step_matcher26 module.register_type = matcher_factory.register_type27# -----------------------------------------------------------------------------28# FAKE MODULE CLASSES: For step imports29# -----------------------------------------------------------------------------30# class FakeModule(object):31class FakeModule(ModuleType):32 ensure_fake = True33 # -- SUPPORT FOR: behave.step_registry.setup_step_decorators()34 def __setitem__(self, name, value):35 assert "." not in name36 setattr(self, name, value)37class StepRegistryModule(FakeModule):38 """Provides a fake :mod:`behave.step_registry` module39 that can be used during step imports.40 """41 __all__ = [42 "given", "when", "then", "step",43 "Given", "When", "Then", "Step",44 ]45 def __init__(self, step_registry):46 super(StepRegistryModule, self).__init__("behave.step_registry")47 self.registry = step_registry48 setup_api_with_step_decorators(self, step_registry)49class StepMatchersModule(FakeModule):50 __all__ = ["use_step_matcher", "register_type", "step_matcher"]51 def __init__(self, matcher_factory):52 super(StepMatchersModule, self).__init__("behave.matchers")53 self.matcher_factory = matcher_factory54 setup_api_with_matcher_functions(self, matcher_factory)55 self.use_default_step_matcher = matcher_factory.use_default_step_matcher56 self.get_matcher = matcher_factory.make_matcher57 # self.matcher_mapping = matcher_mapping or _matchers.matcher_mapping.copy()58 # self.current_matcher = current_matcher or _matchers.current_matcher59 # -- INJECT PYTHON PACKAGE META-DATA:60 # REQUIRED-FOR: Non-fake submodule imports (__path__).61 here = os.path.dirname(__file__)62 self.__file__ = os.path.abspath(os.path.join(here, "matchers.py"))63 self.__name__ = "behave.matchers"64 # self.__path__ = [os.path.abspath(here)]65 # def use_step_matcher(self, name):66 # self.matcher_factory.use_step_matcher(name)67 # # self.current_matcher = self.matcher_mapping[name]68 #69 # def use_default_step_matcher(self, name=None):70 # self.matcher_factory.use_default_step_matcher(name=None)71 #72 # def get_matcher(self, func, pattern):73 # # return self.current_matcher74 # return self.matcher_factory.make_matcher(func, pattern)75 #76 # def register_type(self, **kwargs):77 # # _matchers.register_type(**kwargs)78 # self.matcher_factory.register_type(**kwargs)79 #80 # step_matcher = use_step_matcher81class BehaveModule(FakeModule):82 __all__ = StepRegistryModule.__all__ + StepMatchersModule.__all__83 def __init__(self, step_registry, matcher_factory=None):84 if matcher_factory is None:85 matcher_factory = step_registry.step_matcher_factory86 assert matcher_factory is not None87 super(BehaveModule, self).__init__("behave")88 setup_api_with_step_decorators(self, step_registry)89 setup_api_with_matcher_functions(self, matcher_factory)90 self.use_default_step_matcher = matcher_factory.use_default_step_matcher91 assert step_registry.matcher_factory == matcher_factory92 # -- INJECT PYTHON PACKAGE META-DATA:93 # REQUIRED-FOR: Non-fake submodule imports (__path__).94 here = os.path.dirname(__file__)95 self.__file__ = os.path.abspath(os.path.join(here, "__init__.py"))96 self.__name__ = "behave"97 self.__path__ = [os.path.abspath(here)]98 self.__package__ = None99class StepImportModuleContext(object):100 def __init__(self, step_container):101 self.step_registry = step_container.step_registry102 self.matcher_factory = step_container.matcher_factory103 assert self.step_registry.matcher_factory == self.matcher_factory104 self.step_registry.matcher_factory = self.matcher_factory105 step_registry_module = StepRegistryModule(self.step_registry)106 step_matchers_module = StepMatchersModule(self.matcher_factory)107 behave_module = BehaveModule(self.step_registry, self.matcher_factory)108 self.modules = {109 "behave": behave_module,110 "behave.matchers": step_matchers_module,111 "behave.step_registry": step_registry_module,112 }113 # self.default_matcher = self.step_matchers_module.current_matcher114 def reset_current_matcher(self):115 self.matcher_factory.use_default_step_matcher()116_step_import_lock = Lock()117unknown = object()118@contextmanager119def use_step_import_modules(step_container):120 """Redirect any step/type registration to the runner's step-context object121 during step imports by using fake modules (instead of using module-globals).122 This allows that multiple runners can be used without polluting the123 global variables in problematic modules124 (:mod:`behave.step_registry`, mod:`behave.matchers`).125 .. sourcecode:: python126 # -- RUNNER-IMPLEMENTATION:127 def load_step_definitions(self, ...):128 step_container = self.step_container129 with use_step_import_modules(step_container) as import_context:130 # -- USE: Fake modules during step imports131 ...132 import_context.reset_current_matcher()133 :param step_container: Step context object with step_registry, matcher_factory.134 """135 orig_modules = {}136 import_context = StepImportModuleContext(step_container)137 with _step_import_lock:138 # -- CRITICAL-SECTION (multi-threading protected)139 try:140 # -- SCOPE-GUARD SETUP: Replace original modules with fake ones.141 for module_name, fake_module in six.iteritems(import_context.modules):142 orig_module = sys.modules.get(module_name, unknown)143 orig_modules[module_name] = orig_module144 sys.modules[module_name] = fake_module145 # -- USE: Fake modules for step imports.146 yield import_context147 finally:148 # -- SCOPE-GUARD CLEANUP: Restore original modules.149 for module_name, orig_module in six.iteritems(orig_modules):150 if orig_module is unknown:151 del sys.modules[module_name]152 else:...

Full Screen

Full Screen

behave.step_durations.py

Source:behave.step_durations.py Github

copy

Full Screen

...27 self.max_duration = 028 self.durations = []29 self.step = step30 if step:31 self.process_step(step)32 @staticmethod33 def make_step_name(step):34 step_name = "%s %s" % (step.step_type.capitalize(), step.name)35 return step_name36 def process_step(self, step):37 step_name = self.make_step_name(step)38 if not self.step_name:39 self.step_name = step_name40 if self.min_duration > step.duration:41 self.min_duration = step.duration42 if self.max_duration < step.duration:43 self.max_duration = step.duration44 self.durations.append(step.duration)45class BehaveDurationData(object):46 def __init__(self):47 self.step_registry = {}48 self.all_steps = []49 self.all_scenarios = []50 def process_features(self, features):51 for feature in features:52 self.process_feature(feature)53 def process_feature(self, feature):54 if feature.background:55 self.process_background(feature.background)56 for scenario in feature.scenarios:57 if isinstance(scenario, ScenarioOutline):58 self.process_scenario_outline(scenario)59 else:60 self.process_scenario(scenario)61 def process_step(self, step):62 step_name = StepDurationData.make_step_name(step)63 known_step = self.step_registry.get(step_name, None)64 if known_step:65 known_step.process_step(step)66 else:67 step_data = StepDurationData(step)68 self.step_registry[step_name] = step_data69 self.all_steps.append(step)70 def process_background(self, scenario):71 for step in scenario:72 self.process_step(step)73 def process_scenario(self, scenario):74 for step in scenario:75 self.process_step(step)76 def process_scenario_outline(self, scenario_outline):77 for scenario in scenario_outline:78 self.process_scenario(scenario)79 def report_step_durations(self, limit=None, min_duration=None, ostream=sys.stdout):80 step_datas = list(self.step_registry.values())81 steps_size = len(step_datas)82 steps_by_longest_duration_first = sorted(step_datas,83 key=attrgetter("max_duration"),84 reverse=True)85 ostream.write("STEP DURATIONS (longest first, size=%d):\n" % steps_size)86 ostream.write("-" * 80)87 ostream.write("\n")88 for index, step in enumerate(steps_by_longest_duration_first):89 ostream.write("% 4d. %9.6fs %s" % \...

Full Screen

Full Screen

step_registry.py

Source:step_registry.py Github

copy

Full Screen

1# -*- coding: UTF-8 -*-2"""3Provides a step registry and step decorators.4The step registry allows to match steps (model elements) with5step implementations (step definitions). This is necessary to execute steps.6"""7from __future__ import absolute_import8from behave.matchers import Match, get_matcher9from behave.textutil import text as _text10# limit import * to just the decorators11# pylint: disable=undefined-all-variable12# names = "given when then step"13# names = names + " " + names.title()14# __all__ = names.split()15__all__ = [16 "given", "when", "then", "step", # PREFERRED.17 "Given", "When", "Then", "Step" # Also possible.18]19class AmbiguousStep(ValueError):20 pass21class StepRegistry(object):22 def __init__(self):23 self.steps = {24 "given": [],25 "when": [],26 "then": [],27 "step": [],28 }29 @staticmethod30 def same_step_definition(step, other_pattern, other_location):31 return (step.pattern == other_pattern and32 step.location == other_location and33 other_location.filename != "<string>")34 def add_step_definition(self, keyword, step_text, func):35 step_location = Match.make_location(func)36 step_type = keyword.lower()37 step_text = _text(step_text)38 step_definitions = self.steps[step_type]39 for existing in step_definitions:40 if self.same_step_definition(existing, step_text, step_location):41 # -- EXACT-STEP: Same step function is already registered.42 # This may occur when a step module imports another one.43 return44 elif existing.match(step_text): # -- SIMPLISTIC45 message = u"%s has already been defined in\n existing step %s"46 new_step = u"@%s('%s')" % (step_type, step_text)47 existing.step_type = step_type48 existing_step = existing.describe()49 existing_step += u" at %s" % existing.location50 raise AmbiguousStep(message % (new_step, existing_step))51 step_definitions.append(get_matcher(func, step_text))52 def find_step_definition(self, step):53 candidates = self.steps[step.step_type]54 more_steps = self.steps["step"]55 if step.step_type != "step" and more_steps:56 # -- ENSURE: self.step_type lists are not modified/extended.57 candidates = list(candidates)58 candidates += more_steps59 for step_definition in candidates:60 if step_definition.match(step.name):61 return step_definition62 return None63 def find_match(self, step):64 candidates = self.steps[step.step_type]65 more_steps = self.steps["step"]66 if step.step_type != "step" and more_steps:67 # -- ENSURE: self.step_type lists are not modified/extended.68 candidates = list(candidates)69 candidates += more_steps70 for step_definition in candidates:71 result = step_definition.match(step.name)72 if result:73 return result74 return None75 def make_decorator(self, step_type):76 def decorator(step_text):77 def wrapper(func):78 self.add_step_definition(step_type, step_text, func)79 return func80 return wrapper81 return decorator82registry = StepRegistry()83# -- Create the decorators84# pylint: disable=redefined-outer-name85def setup_step_decorators(run_context=None, registry=registry):86 if run_context is None:87 run_context = globals()88 for step_type in ("given", "when", "then", "step"):89 step_decorator = registry.make_decorator(step_type)90 run_context[step_type.title()] = run_context[step_type] = step_decorator91# -----------------------------------------------------------------------------92# MODULE INIT:93# -----------------------------------------------------------------------------...

Full Screen

Full Screen

main.py

Source:main.py Github

copy

Full Screen

1import sys, string, os2from Tkinter import *3import steps4Plugins = sys.argv[1]5print Plugins6pluginList = Plugins.split(':')7maxStep = 28curStep = 19stepList = []10# functions11# show step on the num index12def showStep(num):13 global stepList14 stepNum = len(stepList)15 if num >= stepNum or num <= 0 :16 pass17 18 i = 019 while i < stepNum:20 if i == num:21 stepList[i].stepFrame.pack(fill=BOTH, anchor='nw')22 else:23 stepList[i].stepFrame.pack_forget()24 i += 125# update the pre & next buttons status26def updateBtnState():27 global curStep28 global btnNextStep29 global btnPreStep30 if curStep == 1:31 btnPreStep['state'] = DISABLED32 btnNextStep['state'] = NORMAL33 btnNextStep['text'] = 'Next'34 elif curStep == maxStep:35 btnPreStep['state'] = NORMAL36 btnNextStep['state'] = NORMAL37 btnNextStep['text'] = 'Finish'38 else:39 btnPreStep['state'] = NORMAL40 btnNextStep['state'] = NORMAL41 btnNextStep['text'] = 'Next'42# next button clicked43def nextStep():44 if btnNextStep['text'] == 'close':45 root.quit()46 return47 48 global curStep49 nowStepObj = stepList[curStep - 1]50 bRet = nowStepObj.checkStep()51 if bRet != None:52 stepError['text'] = bRet53 return54 else:55 stepError['text'] = ''56 57 if curStep < maxStep:58 curStep += 159 showStep(curStep - 1)60 updateBtnState()61 elif curStep == maxStep:62 # disable buttons when process63 btnPreStep['state'] = DISABLED64 btnNextStep['state'] = DISABLED65 66 # get user input arguments67 projPath = stepList[0].getPath()68 plugins = stepList[1].getSelectedPlugins()69 strPlugins = ''70 i = 071 while i < len(plugins):72 strPlugins += "plugins/"73 strPlugins += plugins[i]74 if i != (len(plugins) - 1):75 strPlugins += ':'76 i += 177 78 # process shell script to modify the game project79 ret = os.system('bash ./toolsForGame/addPluginForGame.sh ' + projPath + ' ' + strPlugins)80 81 if ret != 0:82 # enable buttons after process83 btnPreStep['state'] = NORMAL84 btnNextStep['state'] = NORMAL85 stepError['text'] = 'Error during process'86 else:87 # enable next button & change text to close88 btnNextStep['state'] = NORMAL89 btnNextStep['text'] = 'close'90 stepError['text'] = 'Process Successful!'91# pre button clicked92def preStep():93 global curStep94 global stepError95 stepError['text'] = ''96 if curStep > 1:97 curStep -= 198 showStep(curStep - 1)99 100 updateBtnState()101# init root view102root = Tk()103root.title('Plugin-x Integration Guide')104root.geometry("600x400")105rootFrame = Frame(root)106rootFrame.pack(fill=BOTH)107# steps view108MyStep1 = steps.step1()109MyStep1.initStep(rootFrame)110MyStep2 = steps.step2()111MyStep2.initStep(rootFrame, pluginList)112stepList.append(MyStep1)113stepList.append(MyStep2)114MyStep1.stepFrame.pack(fill=BOTH, anchor='nw')115# add step error message116controlFrame = Frame(root)117controlFrame.pack(side=BOTTOM, fill=X, anchor='s')118stepError = Label(controlFrame)119stepError.pack(side=LEFT, padx=30)120# add step button121btnNextStep = Button(controlFrame, text='Next', command=nextStep)122btnPreStep = Button(controlFrame, text='Back', command=preStep, state=DISABLED)123btnNextStep.pack(side=RIGHT, padx=30)124btnPreStep.pack(side=RIGHT)...

Full Screen

Full Screen

behave_undefined_steps.py

Source:behave_undefined_steps.py Github

copy

Full Screen

1# -*- coding: UTF-8 -*-2"""3Provides step definitions for behave based on behave4cmd.4REQUIRES:5 * behave4cmd.steplib.output steps (command output from behave).6"""7from __future__ import absolute_import8from behave import then9from behave.runner_util import make_undefined_step_snippet10# -----------------------------------------------------------------------------11# UTILITY FUNCTIONS:12# -----------------------------------------------------------------------------13def text_indent(text, indent_size=0):14 prefix = " " * indent_size15 return prefix.join(text.splitlines(True))16# -----------------------------------------------------------------------------17# STEPS FOR: Undefined step definitions18# -----------------------------------------------------------------------------19@then(u'an undefined-step snippets section exists')20def step_undefined_step_snippets_section_exists(context):21 """22 Checks if an undefined-step snippet section is in behave command output.23 """24 context.execute_steps(u'''25 Then the command output should contain:26 """27 You can implement step definitions for undefined steps with these snippets:28 """29 ''')30@then(u'an undefined-step snippet should exist for "{step}"')31def step_undefined_step_snippet_should_exist_for(context, step):32 """33 Checks if an undefined-step snippet is provided for a step34 in behave command output (last command).35 EXAMPLE:36 Then an undefined-step snippet should exist for "Given an undefined step"37 """38 undefined_step_snippet = make_undefined_step_snippet(step)39 context.execute_steps(u'''\40Then the command output should contain:41 """42 {undefined_step_snippet}43 """44 '''.format(undefined_step_snippet=text_indent(undefined_step_snippet, 4)))45@then(u'an undefined-step snippet should not exist for "{step}"')46def step_undefined_step_snippet_should_not_exist_for(context, step):47 """48 Checks if an undefined-step snippet is provided for a step49 in behave command output (last command).50 """51 undefined_step_snippet = make_undefined_step_snippet(step)52 context.execute_steps(u'''\53Then the command output should not contain:54 """55 {undefined_step_snippet}56 """57 '''.format(undefined_step_snippet=text_indent(undefined_step_snippet, 4)))58@then(u'undefined-step snippets should exist for')59def step_undefined_step_snippets_should_exist_for_table(context):60 """61 Checks if undefined-step snippets are provided.62 EXAMPLE:63 Then undefined-step snippets should exist for:64 | Step |65 | When an undefined step is used |66 | Then another undefined step is used |67 """68 assert context.table, "REQUIRES: table"69 for row in context.table.rows:70 step = row["Step"]71 step_undefined_step_snippet_should_exist_for(context, step)72@then(u'undefined-step snippets should not exist for')73def step_undefined_step_snippets_should_not_exist_for_table(context):74 """75 Checks if undefined-step snippets are not provided.76 EXAMPLE:77 Then undefined-step snippets should not exist for:78 | Step |79 | When an known step is used |80 | Then another known step is used |81 """82 assert context.table, "REQUIRES: table"83 for row in context.table.rows:84 step = row["Step"]...

Full Screen

Full Screen

test_step_registry.py

Source:test_step_registry.py Github

copy

Full Screen

1# -*- coding: UTF-8 -*-2# pylint: disable=unused-wildcard-import3from __future__ import absolute_import, with_statement4from mock import Mock, patch5from nose.tools import * # pylint: disable=wildcard-import6from six.moves import range # pylint: disable=redefined-builtin7from behave import step_registry8class TestStepRegistry(object):9 # pylint: disable=invalid-name, no-self-use10 def test_add_step_definition_adds_to_lowercased_keyword(self):11 registry = step_registry.StepRegistry()12 # -- MONKEYPATCH-PROBLEM:13 # with patch('behave.matchers.get_matcher') as get_matcher:14 with patch('behave.step_registry.get_matcher') as get_matcher:15 func = lambda x: -x16 pattern = 'just a test string'17 magic_object = object()18 get_matcher.return_value = magic_object19 for step_type in list(registry.steps.keys()):20 l = []21 registry.steps[step_type] = l22 registry.add_step_definition(step_type.upper(), pattern, func)23 get_matcher.assert_called_with(func, pattern)24 eq_(l, [magic_object])25 def test_find_match_with_specific_step_type_also_searches_generic(self):26 registry = step_registry.StepRegistry()27 given_mock = Mock()28 given_mock.match.return_value = None29 step_mock = Mock()30 step_mock.match.return_value = None31 registry.steps['given'].append(given_mock)32 registry.steps['step'].append(step_mock)33 step = Mock()34 step.step_type = 'given'35 step.name = 'just a test step'36 assert registry.find_match(step) is None37 given_mock.match.assert_called_with(step.name)38 step_mock.match.assert_called_with(step.name)39 def test_find_match_with_no_match_returns_none(self):40 registry = step_registry.StepRegistry()41 step_defs = [Mock() for x in range(0, 10)]42 for mock in step_defs:43 mock.match.return_value = None44 registry.steps['when'] = step_defs45 step = Mock()46 step.step_type = 'when'47 step.name = 'just a test step'48 assert registry.find_match(step) is None49 def test_find_match_with_a_match_returns_match(self):50 registry = step_registry.StepRegistry()51 step_defs = [Mock() for x in range(0, 10)]52 for mock in step_defs:53 mock.match.return_value = None54 magic_object = object()55 step_defs[5].match.return_value = magic_object56 registry.steps['then'] = step_defs57 step = Mock()58 step.step_type = 'then'59 step.name = 'just a test step'60 assert registry.find_match(step) is magic_object61 for mock in step_defs[6:]:62 eq_(mock.match.call_count, 0)63 # pylint: disable=line-too-long64 @patch.object(step_registry.registry, 'add_step_definition')65 def test_make_step_decorator_ends_up_adding_a_step_definition(self, add_step_definition):66 step_type = object()67 step_pattern = object()68 func = object()69 decorator = step_registry.registry.make_decorator(step_type)70 wrapper = decorator(step_pattern)71 assert wrapper(func) is func...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { renderHook } from '@testing-library/react-hooks';2import useCounter from './useCounter';3const { result } = renderHook(() => useCounter());4result.current.increment();5result.current.increment();6result.current.decrement();7result.current.reset();8result.current.incrementBy(5);

Full Screen

Using AI Code Generation

copy

Full Screen

1import { renderHook } from '@testing-library/react-hooks';2import { useFetch } from './useFetch';3test('should return data', async () => {4 await waitForNextUpdate();5 expect(result.current.data).toBe('some data');6});7import { useState, useEffect } from 'react';8export const useFetch = (url: string) => {9 const [data, setData] = useState(null);10 useEffect(() => {11 fetch(url)12 .then((res) => res.json())13 .then((data) => setData(data));14 }, [url]);15 return { data };16};17import { useFetch } from './useFetch';18export const App = () => {19 return <div>{data}</div>;20};21import { render } from '@testing-library/react';22import { App } from './App';23test('should render data', async () => {24 const { findByText } = render(<App />);25 const data = await findByText('some data');26 expect(data).toBeInTheDocument();27});28import { renderHook } from '@testing-library/react-hooks';29import { useFetch } from './useFetch';30test('should return data', async () => {31 await waitForNextUpdate();32 expect(result.current.data).toBe('some data');33});34import { useState, useEffect } from 'react';35export const useFetch = (url: string) => {36 const [data, setData] = useState(null);37 useEffect(() => {38 fetch(url)39 .then((res) => res.json())40 .then((data) => setData(data));41 }, [url]);42 return { data };43};44import { useFetch } from './useFetch';45export const App = () => {46 return <div>{data}</div>;47};

Full Screen

Using AI Code Generation

copy

Full Screen

1import {renderHook, act, cleanup} from '@testing-library/react-hooks'2import {useCounter} from './useCounter'3describe('useCounter', () => {4 afterEach(cleanup)5 it('should increment counter', () => {6 const {result} = renderHook(() => useCounter())7 act(() => {8 result.current.increment()9 })10 expect(result.current.count).toBe(1)11 })12 it('should decrement counter', () => {13 const {result} = renderHook(() => useCounter())14 act(() => {15 result.current.decrement()16 })17 expect(result.current.count).toBe(-1)18 })19})20import {useState} from 'react'21export function useCounter() {22 const [count, setCount] = useState(0)23 const increment = () => setCount(count + 1)24 const decrement = () => setCount(count - 1)25 return {count, increment, decrement}26}27import React from 'react'28import ReactDOM from 'react-dom'29import App from './App'30ReactDOM.render(<App />, document.getElementById('root'))31import React from 'react'32import {useCounter} from './useCounter'33function App() {34 const {count, increment, decrement} = useCounter()35 return (36 <button onClick={decrement}>-</button>37 <span>{count}</span>38 <button onClick={increment}>+</button>39}40import {useState} from 'react'41export function useCounter() {42 const [count, setCount] = useState(0)43 const increment = () => setCount(count + 1)44 const decrement = () => setCount(count - 1)45 return {count, increment, decrement}46}

Full Screen

Using AI Code Generation

copy

Full Screen

1import { renderHook } from "@testing-library/react-hooks";2import { useCounter } from "./counter";3test("counter increments on click", () => {4 const { result } = renderHook(() => useCounter());5 expect(result.current.count).toBe(0);6 act(() => {7 result.current.increment();8 });9 expect(result.current.count).toBe(1);10});11import { useState } from "react";12export function useCounter() {13 const [count, setCount] = useState(0);14 const increment = () => setCount(count + 1);15 return { count, increment };16}17import { useCounter } from "./counter";18function App() {19 const { count, increment } = useCounter();20 return (21 <h1 data-testid="counter">{count}</h1>22 <button onClick={increment}>Increment</button>23 );24}25export default App;26import { render, screen } from "@testing-library/react";27import App from "./App";28test("renders counter", () => {29 render(<App />);30 const counterElement = screen.getByTestId("counter");31 expect(counterElement).toBeInTheDocument();32 expect(counterElement).toHaveTextContent("0");33});34import { render, screen } from "@testing-library/react";35import App from "./App";36test("renders counter", () => {37 render(<App />);38 const counterElement = screen.getByTestId("counter");39 expect(counterElement).toBeInTheDocument();40 expect(counterElement).toHaveTextContent("0");41});42import { render, screen } from "@testing-library/react";43import App from "./App";44test("renders counter", () => {45 render(<App />);46 const counterElement = screen.getByTestId("counter");47 expect(counterElement).toBeInTheDocument();48 expect(counterElement).toHaveTextContent("0");49});50import { render, screen } from "@testing

Full Screen

Using AI Code Generation

copy

Full Screen

1import { renderHook, act } from '@testing-library/react-hooks'2import { useCounter } from './useCounter'3describe('useCounter', () => {4 it('should increment the counter', () => {5 const { result } = renderHook(() => useCounter())6 act(() => {7 result.current.increment()8 })9 expect(result.current.count).toBe(1)10 })11})

Full Screen

Using AI Code Generation

copy

Full Screen

1import { renderHook, act } from '@testing-library/react-hooks';2import { useCounter } from './counter';3test('should increment counter by 1', () => {4 const { result } = renderHook(() => useCounter());5 act(() => {6 result.current.step(1);7 });8 expect(result.current.count).toBe(1);9});10import { useState } from 'react';11export function useCounter() {12 const [count, setCount] = useState(0);13 const step = (val) => {14 setCount(count + val);15 };16 return { count, step };17}

Full Screen

Using AI Code Generation

copy

Full Screen

1const { result, waitForNextUpdate } = renderHook(() => useFetch(url));2await waitForNextUpdate();3expect(result.current.data).toEqual({ hello: 'world' });4const { result, waitForNextUpdate } = renderHook(() => useFetch(url));5await waitForNextUpdate();6expect(result.current.data).toEqual({ hello: 'world' });7const { result, waitForNextUpdate } = renderHook(() => useFetch(url));8await waitForNextUpdate();9expect(result.current.data).toEqual({ hello: 'world' });10const { result, waitForNextUpdate } = renderHook(() => useFetch(url));11await waitForNextUpdate();12expect(result.current.data).toEqual({ hello: 'world' });13const { result, waitForNextUpdate } = renderHook(() => useFetch(url));14await waitForNextUpdate();15expect(result.current.data).toEqual({ hello: 'world' });16const { result, waitForNextUpdate } = renderHook(() => useFetch(url));17await waitForNextUpdate();18expect(result.current.data).toEqual({ hello: 'world' });19const { result, waitForNextUpdate } = renderHook(() => useFetch(url));20await waitForNextUpdate();21expect(result.current.data).toEqual({ hello: 'world' });22const { result, waitForNextUpdate } = renderHook(() => useFetch(url));23await waitForNextUpdate();24expect(result.current.data).toEqual({ hello: 'world' });25const { result, waitForNextUpdate } = renderHook(() => useFetch(url));26await waitForNextUpdate();27expect(result

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run testing-library-react-hooks automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful