Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def make_context():
return Context(runner=Runner(config={}))
def make_runtime_context(runner=None):
"""Build a runtime/runner context for the tests here (partly faked).
:return: Runtime context object (normally used by the runner).
"""
if runner is None:
runner = Mock()
runner.config = Mock()
return Context(runner)
def test_async_step_fails(self):
"""ENSURE: Failures in async-steps are detected correctly."""
step_container = SimpleStepContainer()
with use_step_import_modules(step_container):
# -- STEP-DEFINITIONS EXAMPLE (as MODULE SNIPPET):
# VARIANT 1: Use async def step_impl()
from behave import when
from behave.api.async_step import async_run_until_complete
@when('an async-step fails')
@async_run_until_complete
async def when_async_step_fails(context):
assert False, "XFAIL in async-step"
# -- RUN ASYNC-STEP: Verify that AssertionError is detected.
context = Context(runner=Runner(config={}))
with pytest.raises(AssertionError):
when_async_step_fails(context)
def test_positional_arguments(self):
text = "has a {}, an {:d} and a {:f}"
matcher = ParseMatcher(self.record_args, text)
context = runner.Context(Mock())
m = matcher.match("has a foo, an 11 and a 3.14159")
m.run(context)
assert self.recorded_args == ((context, 'foo', 11, 3.14159), {})
def test_add_cleanup__rejects_noncallable_cleanup_func(self):
class NonCallable(object): pass
non_callable = NonCallable()
context = Context(runner=Mock())
with pytest.raises(AssertionError) as e:
with scoped_context_layer(context):
context.add_cleanup(non_callable)
assert "REQUIRES: callable(cleanup_func)" in str(e.value)
def test_run_hook_runs_a_hook_that_exists(self):
config = Mock()
r = runner.Runner(config)
# XXX r.config = Mock()
r.config.stdout_capture = False
r.config.stderr_capture = False
r.config.dry_run = False
r.hooks["before_lunch"] = hook = Mock()
args = (runner.Context(Mock()), Mock(), Mock())
r.run_hook("before_lunch", *args)
hook.assert_called_with(*args)
self.step_registry = StepRegistry()
ExampleSteps.register_steps_with(self.step_registry)
ExampleSteps.text = None
ExampleSteps.table = None
runner_ = Mock()
self.config = runner_.config = Mock()
runner_.config.verbose = False
runner_.config.stdout_capture = False
runner_.config.stderr_capture = False
runner_.config.log_capture = False
runner_.config.logging_format = None
runner_.config.logging_datefmt = None
runner_.step_registry = self.step_registry
self.context = runner.Context(runner_)
runner_.context = self.context
self.context.feature = Mock()
self.context.feature.parser = parser.Parser()
self.context.runner = runner_
# self.context.text = None
def run_model(self, features=None):
# pylint: disable=too-many-branches
if not self.context:
self.context = Context(self)
if self.step_registry is None:
self.step_registry = the_step_registry
if features is None:
features = self.features
# -- ENSURE: context.execute_steps() works in weird cases (hooks, ...)
context = self.context
self.hook_failures = 0
self.setup_capture()
self.run_hook("before_all", context)
run_feature = not self.aborted
failed_count = 0
undefined_steps_initial_size = len(self.undefined_steps)
for feature in features:
if run_feature:
import os
import django
from behave.runner import Context
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.core.management import call_command
from django.shortcuts import resolve_url
from django.test.runner import DiscoverRunner
from splinter.browser import Browser
os.environ["DJANGO_SETTINGS_MODULE"] = "myrecommendations.settings"
class ExtendedContext(Context):
def get_url(self, to=None, *args, **kwargs):
return self.test.live_server_url + (
resolve_url(to, *args, **kwargs) if to else '')
def before_all(context):
django.setup()
context.test_runner = DiscoverRunner()
context.test_runner.setup_test_environment()
context.browser = Browser('chrome', headless=True)
def before_scenario(context, scenario):
context.test_runner.setup_databases()
object.__setattr__(context, '__class__', ExtendedContext)
context.test = StaticLiveServerTestCase
context.test.setUpClass()
from copy import copy
from behave import step_registry as module_step_registry
from behave.runner import ModelRunner, Context
from django.shortcuts import resolve_url
class PatchedContext(Context):
@property
def base_url(self):
try:
return self.test.live_server_url
except AttributeError:
raise RuntimeError('Web browser automation is not available. '
'This scenario step can not be run with the '
'--simple or -S flag.')
def get_url(self, to=None, *args, **kwargs):
return self.base_url + (
resolve_url(to, *args, **kwargs) if to else '')
def load_registered_fixtures(context):