Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_data_schema1(self):
@fixture
def foo(context, *args, **kwargs):
# -- NOTE checkpoints: Injected from outer scope.
checkpoints.append("foo.setup")
yield "fixture:foo"
checkpoints.append("foo.cleanup")
fixture_registry = {
"fixture.foo": foo,
}
# -- PERFORM-TEST:
context = make_runtime_context()
checkpoints = []
with scoped_context_layer(context):
use_fixture_by_tag("fixture.foo", context, fixture_registry)
checkpoints.append("scoped-block")
# -- VERIFY:
assert checkpoints == [
"foo.setup", "scoped-block", "foo.cleanup"
]
yield
checkpoints.append("foo.cleanup:%s" % fixture_name)
@fixture
def composite2(context, checkpoints, *args, **kwargs):
the_composite = use_composite_fixture_with(context, [
fixture_call_params(fixture_foo, checkpoints, name="_1"),
fixture_call_params(fixture_foo, checkpoints, name="_2"),
])
return the_composite
# -- PERFORM-TEST:
checkpoints = []
context = make_runtime_context()
with pytest.raises(RuntimeError):
with scoped_context_layer(context):
use_fixture(composite2, context, checkpoints)
checkpoints.append("scoped-block_with_error")
raise RuntimeError("OOPS")
checkpoints.append("scoped-block.done:NOT_REACHED")
# -- ENSURES:
# * fixture1-cleanup/cleanup is called even scoped-block-error
# * fixture2-cleanup/cleanup is called even scoped-block-error
# * fixture-cleanup occurs in reversed setup-order
assert checkpoints == [
"foo.setup:_1", "foo.setup:_2",
"scoped-block_with_error",
"foo.cleanup:_2", "foo.cleanup:_1"
]
def test_with_function(self):
@fixture
def bar(context, checkpoints, *args, **kwargs):
checkpoints.append("bar.setup")
fixture_object = BarFixture.setup(*args, **kwargs)
context.bar = fixture_object
return fixture_object
checkpoints = []
context = make_runtime_context()
with scoped_context_layer(context):
the_fixture = use_fixture(bar, context, checkpoints)
assert_context_setup(context, "bar", BarFixture)
assert_fixture_setup_called(the_fixture)
assert_fixture_cleanup_not_called(the_fixture)
assert checkpoints == ["bar.setup"]
print("Do something...")
assert_context_cleanup(context, "foo")
assert_fixture_cleanup_not_called(the_fixture)
# -- NOT: Normal functions have no fixture-cleanup part.
def test_block_eror_with_context_cleanup_then_cleanup_is_called(self):
# -- CASE: Fixture is normal-function
@fixture
def bar(context, checkpoints):
def cleanup_bar():
checkpoints.append("cleanup_bar")
checkpoints.append("bar.setup")
context.add_cleanup(cleanup_bar)
checkpoints = []
context = make_runtime_context()
with pytest.raises(RuntimeError):
with scoped_context_layer(context):
use_fixture(bar, context, checkpoints)
checkpoints.append("scoped-block_with_error")
raise RuntimeError("scoped-block")
checkpoints.append("NOT_REACHED")
# -- ENSURE:
assert checkpoints == [
"bar.setup", "scoped-block_with_error", "cleanup_bar"
]
def test_issue__getattr_with_protected_unknown_context_attribute_raises_no_error():
context = Context(runner=Mock())
with scoped_context_layer(context): # CALLS-HERE: context._push()
value = getattr(context, "_UNKNOWN_ATTRIB", "__UNKNOWN__")
assert value == "__UNKNOWN__"
# -- ENSURED: No exception is raised, neither KeyError nor AttributeError
def test_with_generator_function(self):
@fixture
def foo(context, checkpoints, *args, **kwargs):
checkpoints.append("foo.setup")
fixture_object = FooFixture.setup(*args, **kwargs)
context.foo = fixture_object
yield fixture_object
fixture_object.cleanup()
checkpoints.append("foo.cleanup.done")
checkpoints = []
context = make_runtime_context()
with scoped_context_layer(context):
the_fixture = use_fixture(foo, context, checkpoints)
assert_context_setup(context, "foo", FooFixture)
assert_fixture_setup_called(the_fixture)
assert_fixture_cleanup_not_called(the_fixture)
assert checkpoints == ["foo.setup"]
print("Do something...")
assert_context_cleanup(context, "foo")
assert_fixture_cleanup_called(the_fixture)
assert checkpoints == ["foo.setup", "foo.cleanup.done"]
fixture_name = kwargs.get("name", "foo")
checkpoints.append("foo.setup:%s" % fixture_name)
yield
checkpoints.append("foo.cleanup:%s" % fixture_name)
@fixture
def composite2(context, checkpoints, *args, **kwargs):
the_fixture1 = use_fixture(fixture_foo, context, checkpoints, name="_1")
the_fixture2 = use_fixture(fixture_foo, context, checkpoints, name="_2")
return (the_fixture1, the_fixture2)
# -- PERFORM-TEST:
context = make_runtime_context()
checkpoints = []
with pytest.raises(RuntimeError):
with scoped_context_layer(context):
use_fixture(composite2, context, checkpoints)
checkpoints.append("scoped-block_with_error")
raise RuntimeError("OOPS")
checkpoints.append("scoped-block.done:NOT_REACHED")
# -- ENSURES:
# * fixture1-cleanup/cleanup is called even scoped-block-error
# * fixture2-cleanup/cleanup is called even scoped-block-error
# * fixture-cleanup occurs in reversed setup-order
assert checkpoints == [
"foo.setup:_1", "foo.setup:_2",
"scoped-block_with_error",
"foo.cleanup:_2", "foo.cleanup:_1"
]
def test_setup_error_with_context_cleanup1_then_cleanup_is_called(self):
# -- CASE: Fixture is normal function
@fixture
def foo(context, checkpoints):
def cleanup_foo(arg=""):
checkpoints.append("cleanup_foo:%s" % arg)
checkpoints.append("foo.setup_with_error:foo_1")
context.add_cleanup(cleanup_foo, "foo_1")
raise FixtureSetupError("foo")
checkpoints.append("foo.setup.done:NOT_REACHED")
checkpoints = []
context = make_runtime_context()
with pytest.raises(FixtureSetupError):
with scoped_context_layer(context):
use_fixture(foo, context, checkpoints)
checkpoints.append("scoped-block:NOT_REACHED")
# -- ENSURE: cleanup_foo() is called (LATE-CLEANUP on scope-exit)
assert checkpoints == ["foo.setup_with_error:foo_1", "cleanup_foo:foo_1"]
pass
class BadFixtureData(object):
def __init__(self, fixture_func, *args, **kwargs):
self.fixture_func = fixture_func
self.fixture_args = args
self.fixture_kwargs = kwargs
fixture_registry = {
"fixture.foo": BadFixtureData(foo, 1, 2, 3, name="foo_1")
}
# -- PERFORM-TEST:
context = make_runtime_context()
with pytest.raises(ValueError) as exc_info:
with scoped_context_layer(context):
use_fixture_by_tag("fixture.foo", context, fixture_registry)
# -- VERIFY:
expected = "fixture_data: Expected tuple or fixture-func, but is:"
assert expected in str(exc_info.value)
assert "BadFixtureData object" in str(exc_info.value)
def test_fixture_with_kwargs(self):
"""Ensures that keyword args are passed to fixture function."""
@fixture
def bar(context, *args, **kwargs):
fixture_object = BarFixture.setup(*args, **kwargs)
context.bar = fixture_object
return fixture_object
context = make_runtime_context()
with scoped_context_layer(context):
the_fixture = use_fixture(bar, context, name="bar", timeout=10)
expected_kwargs = dict(name="bar", timeout=10)
assert the_fixture.kwargs == expected_kwargs