Best Python code snippet using behave
runner_util.py
Source:runner_util.py
...333 step_globals = {334 "use_step_matcher": matchers.use_step_matcher,335 "step_matcher": matchers.step_matcher, # -- DEPRECATING336 }337 setup_step_decorators(step_globals)338 # -- Allow steps to import other stuff from the steps dir339 # NOTE: Default matcher can be overridden in "environment.py" hook.340 with PathManager(step_paths):341 default_matcher = matchers.current_matcher342 for path in step_paths:343 for name in sorted(os.listdir(path)):344 if name.endswith(".py"):345 # -- LOAD STEP DEFINITION:346 # Reset to default matcher after each step-definition.347 # A step-definition may change the matcher 0..N times.348 # ENSURE: Each step definition has clean globals.349 # try:350 step_module_globals = step_globals.copy()351 exec_file(os.path.join(path, name), step_module_globals)352 matchers.current_matcher = default_matcher353def make_undefined_step_snippet(step, language=None):354 """Helper function to create an undefined-step snippet for a step.355 :param step: Step to use (as Step object or string).356 :param language: i18n language, optionally needed for step text parsing.357 :return: Undefined-step snippet (as string).358 """359 if isinstance(step, string_types):360 step_text = step361 steps = parser.parse_steps(step_text, language=language)362 step = steps[0]363 assert step, "ParseError: %s" % step_text364 prefix = u"u"365 single_quote = "'"366 if single_quote in step.name:367 step.name = step.name.replace(single_quote, r"\'")368 schema = u"@%s(%s'%s')\ndef step_impl(context):\n"369 schema += u" raise NotImplementedError(%s'STEP: %s %s')\n\n"370 snippet = schema % (step.step_type, prefix, step.name,371 prefix, step.step_type.title(), step.name)372 return snippet373def make_undefined_step_snippets(undefined_steps, make_snippet=None):374 """Creates a list of undefined step snippets.375 Note that duplicated steps are removed internally.376 :param undefined_steps: List of undefined steps (as Step object or string).377 :param make_snippet: Function that generates snippet (optional)378 :return: List of undefined step snippets (as list of strings)379 """380 if make_snippet is None:381 make_snippet = make_undefined_step_snippet382 # -- NOTE: Remove any duplicated undefined steps.383 step_snippets = []384 collected_steps = set()385 for undefined_step in undefined_steps:386 if undefined_step in collected_steps:387 continue388 collected_steps.add(undefined_step)389 step_snippet = make_snippet(undefined_step)390 step_snippets.append(step_snippet)391 return step_snippets392def print_undefined_step_snippets(undefined_steps, stream=None, colored=True):393 """394 Print snippets for the undefined steps that were discovered.395 :param undefined_steps: List of undefined steps (as list<string>).396 :param stream: Output stream to use (default: sys.stderr).397 :param colored: Indicates if coloring should be used (default: True)398 """399 if not undefined_steps:400 return401 if not stream:402 stream = sys.stderr403 msg = u"\nYou can implement step definitions for undefined steps with "404 msg += u"these snippets:\n\n"405 msg += u"\n".join(make_undefined_step_snippets(undefined_steps))406 if colored:407 # -- OOPS: Unclear if stream supports ANSI coloring.408 from behave.formatter.ansi_escapes import escapes409 msg = escapes['undefined'] + msg + escapes['reset']410 stream = ensure_stream_with_encoder(stream)411 stream.write(msg)412 stream.flush()413def reset_runtime():414 """Reset runtime environment.415 Best effort to reset module data to initial state.416 """417 from behave import step_registry418 from behave import matchers419 # -- RESET 1: behave.step_registry420 step_registry.registry = step_registry.StepRegistry()421 step_registry.setup_step_decorators(None, step_registry.registry)422 # -- RESET 2: behave.matchers423 matchers.ParseMatcher.custom_types = {}...
step_registry.py
Source:step_registry.py
...74 return wrapper75 return decorator76registry = StepRegistry()77# -- Create the decorators78def setup_step_decorators(run_context=None, registry=registry):79 if run_context is None:80 run_context = globals()81 for step_type in ('given', 'when', 'then', 'step'):82 step_decorator = registry.make_decorator(step_type)83 run_context[step_type.title()] = run_context[step_type] = step_decorator84# -----------------------------------------------------------------------------85# MODULE INIT:86# -----------------------------------------------------------------------------87# limit import * to just the decorators88names = 'given when then step'89names = names + ' ' + names.title()90__all__ = names.split()...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!