How to use make_runtime_context method in Behave

Best Python code snippet using behave

test_fixture.py

Source:test_fixture.py Github

copy

Full Screen

...15import six16# -----------------------------------------------------------------------------17# TEST SUPPORT18# -----------------------------------------------------------------------------19def make_runtime_context(runner=None):20 """Build a runtime/runner context for the tests here (partly faked).21 22 :return: Runtime context object (normally used by the runner).23 """24 if runner is None:25 runner = Mock()26 runner.config = Mock()27 return Context(runner)28class BasicFixture(object):29 """Helper class used in behave fixtures (test-support)."""30 def __init__(self, *args, **kwargs):31 setup_called = kwargs.pop("_setup_called", True)32 name = kwargs.get("name", self.__class__.__name__)33 self.name = name34 self.args = args35 self.kwargs = kwargs.copy()36 self.setup_called = setup_called37 self.cleanup_called = False38 def __del__(self):39 if not self.cleanup_called:40 # print("LATE-CLEANUP (in dtor): %s" % self.name)41 self.cleanup()42 @classmethod43 def setup(cls, *args, **kwargs):44 name = kwargs.get("name", cls.__name__)45 print("%s.setup: %s, kwargs=%r" % (cls.__name__, name, kwargs))46 kwargs["_setup_called"] = True47 return cls(*args, **kwargs)48 def cleanup(self):49 self.cleanup_called = True50 print("%s.cleanup: %s" % (self.__class__.__name__, self.name))51 def __str__(self):52 args_text = ", ".join([str(arg) for arg in self.args])53 kwargs_parts = ["%s= %s" % (key, value)54 for key, value in sorted(six.iteritems(self.kwargs))]55 kwargs_text = ", ".join(kwargs_parts)56 return "%s: args=%s; kwargs=%s" % (self.name, args_text, kwargs_text)57class FooFixture(BasicFixture): pass58class BarFixture(BasicFixture): pass59# -- FIXTURE EXCEPTIONS:60class FixtureSetupError(RuntimeError): pass61class FixtureCleanupError(RuntimeError): pass62# -----------------------------------------------------------------------------63# CUSTOM ASSERTIONS:64# -----------------------------------------------------------------------------65def assert_context_setup(context, fixture_name, fixture_class=Unknown):66 """Ensure that fixture object is stored in context."""67 the_fixture = getattr(context, fixture_name, Unknown)68 assert hasattr(context, fixture_name)69 assert the_fixture is not Unknown70 if fixture_class is not Unknown:71 assert isinstance(the_fixture, fixture_class)72def assert_context_cleanup(context, fixture_name):73 """Ensure that fixture object is no longer stored in context."""74 assert not hasattr(context, fixture_name)75def assert_fixture_setup_called(fixture_obj):76 """Ensure that fixture setup was performed."""77 if hasattr(fixture_obj, "setup_called"):78 assert fixture_obj.setup_called79def assert_fixture_cleanup_called(fixture_obj):80 if hasattr(fixture_obj, "cleanup_called"):81 assert fixture_obj.cleanup_called82def assert_fixture_cleanup_not_called(fixture_obj):83 if hasattr(fixture_obj, "cleanup_called"):84 assert not fixture_obj.cleanup_called85# -----------------------------------------------------------------------------86# TEST SUITE:87# -----------------------------------------------------------------------------88class TestFixtureDecorator(object):89 def test_decorator(self):90 @fixture91 def foo(context, *args, **kwargs):92 pass93 assert foo.behave_fixture == True94 assert foo.name is None95 assert foo.pattern is None96 assert callable(foo)97 def test_decorator_call_without_params(self):98 @fixture()99 def bar0(context, *args, **kwargs):100 pass101 assert bar0.behave_fixture == True102 assert bar0.name is None103 assert bar0.pattern is None104 assert callable(bar0)105 def test_decorator_call_with_name(self):106 @fixture(name="fixture.bar2")107 def bar1(context, *args, **kwargs):108 pass109 assert bar1.behave_fixture == True110 assert bar1.name == "fixture.bar2"111 assert bar1.pattern is None112 assert callable(bar1)113 def test_decorated_generator_function_is_callable(self):114 # -- SIMILAR-TO: TestUseFixture.test_with_generator_func()115 @fixture116 def foo(context, *args, **kwargs):117 fixture_object = FooFixture.setup(*args, **kwargs)118 yield fixture_object119 fixture_object.cleanup()120 # -- ENSURE: Decorated fixture function can be called w/ params.121 context = None122 func_it = foo(context, 1, 2, 3, name="foo_1")123 # -- VERIFY: Expectations124 assert is_context_manager(foo)125 assert inspect.isfunction(foo)126 assert inspect.isgenerator(func_it)127 def test_decorated_function_is_callable(self):128 # -- SIMILAR-TO: TestUseFixture.test_with_function()129 @fixture(name="fixture.bar")130 def bar(context, *args, **kwargs):131 fixture_object = BarFixture.setup(*args, **kwargs)132 return fixture_object133 # -- ENSURE: Decorated fixture function can be called w/ params.134 context = None135 the_fixture = bar(context, 3, 2, 1, name="bar_1")136 # -- VERIFY: Expectations137 assert inspect.isfunction(bar)138 assert isinstance(the_fixture, BarFixture)139 def test_decorator_with_non_callable_raises_type_error(self):140 class NotCallable(object): pass141 with pytest.raises(TypeError) as exc_info:142 not_callable = NotCallable()143 bad_fixture = fixture(not_callable) # DECORATED_BY_HAND144 exception_text = str(exc_info.value)145 assert "Invalid func:" in exception_text146 assert "NotCallable object" in exception_text147class TestUseFixture(object):148 def test_basic_lifecycle(self):149 # -- NOTE: Use explicit checks instead of assert-helper functions.150 @fixture151 def foo(context, checkpoints, *args, **kwargs):152 checkpoints.append("foo.setup")153 yield FooFixture()154 checkpoints.append("foo.cleanup")155 checkpoints = []156 context = make_runtime_context()157 with scoped_context_layer(context):158 the_fixture = use_fixture(foo, context, checkpoints)159 # -- ENSURE: Fixture setup is performed (and as expected)160 assert isinstance(the_fixture, FooFixture)161 assert checkpoints == ["foo.setup"]162 checkpoints.append("scoped-block")163 # -- ENSURE: Fixture cleanup was performed164 assert checkpoints == ["foo.setup", "scoped-block", "foo.cleanup"]165 def test_fixture_with_args(self):166 """Ensures that positional args are passed to fixture function."""167 @fixture168 def foo(context, *args, **kwargs):169 fixture_object = FooFixture.setup(*args, **kwargs)170 context.foo = fixture_object171 yield fixture_object172 fixture_object.cleanup()173 context = make_runtime_context()174 with scoped_context_layer(context):175 the_fixture = use_fixture(foo, context, 1, 2, 3)176 expected_args = (1, 2, 3)177 assert the_fixture.args == expected_args178 def test_fixture_with_kwargs(self):179 """Ensures that keyword args are passed to fixture function."""180 @fixture181 def bar(context, *args, **kwargs):182 fixture_object = BarFixture.setup(*args, **kwargs)183 context.bar = fixture_object184 return fixture_object185 context = make_runtime_context()186 with scoped_context_layer(context):187 the_fixture = use_fixture(bar, context, name="bar", timeout=10)188 expected_kwargs = dict(name="bar", timeout=10)189 assert the_fixture.kwargs == expected_kwargs190 def test_with_generator_function(self):191 @fixture192 def foo(context, checkpoints, *args, **kwargs):193 checkpoints.append("foo.setup")194 fixture_object = FooFixture.setup(*args, **kwargs)195 context.foo = fixture_object196 yield fixture_object197 fixture_object.cleanup()198 checkpoints.append("foo.cleanup.done")199 checkpoints = []200 context = make_runtime_context()201 with scoped_context_layer(context):202 the_fixture = use_fixture(foo, context, checkpoints)203 assert_context_setup(context, "foo", FooFixture)204 assert_fixture_setup_called(the_fixture)205 assert_fixture_cleanup_not_called(the_fixture)206 assert checkpoints == ["foo.setup"]207 print("Do something...")208 assert_context_cleanup(context, "foo")209 assert_fixture_cleanup_called(the_fixture)210 assert checkpoints == ["foo.setup", "foo.cleanup.done"]211 def test_with_function(self):212 @fixture213 def bar(context, checkpoints, *args, **kwargs):214 checkpoints.append("bar.setup")215 fixture_object = BarFixture.setup(*args, **kwargs)216 context.bar = fixture_object217 return fixture_object218 checkpoints = []219 context = make_runtime_context()220 with scoped_context_layer(context):221 the_fixture = use_fixture(bar, context, checkpoints)222 assert_context_setup(context, "bar", BarFixture)223 assert_fixture_setup_called(the_fixture)224 assert_fixture_cleanup_not_called(the_fixture)225 assert checkpoints == ["bar.setup"]226 print("Do something...")227 assert_context_cleanup(context, "foo")228 assert_fixture_cleanup_not_called(the_fixture)229 # -- NOT: Normal functions have no fixture-cleanup part.230 def test_can_use_fixture_two_times(self):231 """Ensures that a fixture can be used multiple times 232 (with different names) within a context layer.233 """234 @fixture235 def foo(context, checkpoints, *args, **kwargs):236 fixture_object = FooFixture.setup(*args, **kwargs)237 setattr(context, fixture_object.name, fixture_object)238 checkpoints.append("foo.setup:%s" % fixture_object.name)239 yield fixture_object240 checkpoints.append("foo.cleanup:%s" % fixture_object.name)241 fixture_object.cleanup()242 checkpoints = []243 context = make_runtime_context()244 with scoped_context_layer(context):245 the_fixture1 = use_fixture(foo, context, checkpoints, name="foo_1")246 the_fixture2 = use_fixture(foo, context, checkpoints, name="foo_2")247 # -- VERIFY: Fixture and context setup was performed.248 assert checkpoints == ["foo.setup:foo_1", "foo.setup:foo_2"]249 assert context.foo_1 is the_fixture1250 assert context.foo_2 is the_fixture2251 assert the_fixture1 is not the_fixture2252 checkpoints.append("scoped-block")253 # -- VERIFY: Fixture and context cleanup is performed.254 assert_context_cleanup(context, "foo_1")255 assert_context_cleanup(context, "foo_2")256 assert checkpoints == ["foo.setup:foo_1", "foo.setup:foo_2",257 "scoped-block",258 "foo.cleanup:foo_2", "foo.cleanup:foo_1"]259 # -- NOTE: Cleanups occur in reverse order.260 def test_invalid_fixture_function(self):261 """Test invalid generator function with more than one yield-statement262 (not a valid fixture/context-manager).263 """264 @fixture265 def invalid_fixture(context, checkpoints, *args, **kwargs):266 checkpoints.append("bad.setup")267 yield FooFixture(*args, **kwargs)268 checkpoints.append("bad.cleanup")269 yield None # -- SYNDROME HERE: More than one yield-statement270 # -- PERFORM-TEST:271 checkpoints = []272 context = make_runtime_context()273 with pytest.raises(InvalidFixtureError):274 with scoped_context_layer(context):275 the_fixture = use_fixture(invalid_fixture, context, checkpoints)276 assert checkpoints == ["bad.setup"]277 checkpoints.append("scoped-block")278 # -- VERIFY: Ensure normal cleanup-parts were performed.279 assert checkpoints == ["bad.setup", "scoped-block", "bad.cleanup"]280 def test_bad_with_setup_error(self):281 # -- SAD: cleanup_fixture() part is called when setup-error occurs,282 # but not the cleanup-part of the generator (generator-magic).283 @fixture284 def bad_with_setup_error(context, checkpoints, *args, **kwargs):285 checkpoints.append("bad.setup_with_error")286 raise FixtureSetupError()287 yield FooFixture(*args, **kwargs)288 checkpoints.append("bad.cleanup:NOT_REACHED")289 # -- PERFORM-TEST:290 the_fixture = None291 checkpoints = []292 context = make_runtime_context()293 bad_fixture = bad_with_setup_error294 with pytest.raises(FixtureSetupError):295 with scoped_context_layer(context):296 the_fixture = use_fixture(bad_fixture, context, checkpoints)297 checkpoints.append("scoped-block:NOT_REACHED")298 # -- VERIFY: Ensure normal cleanup-parts were performed.299 # * SAD: fixture-cleanup is not called due to fixture-setup error.300 assert the_fixture is None # -- NEVER STORED: Due to setup error.301 assert checkpoints == ["bad.setup_with_error"]302 def test_bad_with_setup_error_aborts_on_first_error(self):303 @fixture304 def foo(context, checkpoints, *args, **kwargs):305 fixture_name = kwargs.get("name", "")306 checkpoints.append("foo.setup:%s" % fixture_name)307 yield FooFixture(*args, **kwargs)308 checkpoints.append("foo.cleanup:%s" % fixture_name)309 @fixture310 def bad_with_setup_error(context, checkpoints, *args, **kwargs):311 checkpoints.append("bad.setup_with_error")312 raise FixtureSetupError()313 yield FooFixture(*args, **kwargs)314 checkpoints.append("bad.cleanup:NOT_REACHED")315 # -- PERFORM-TEST:316 the_fixture1 = None317 the_fixture2 = None318 the_fixture3 = None319 checkpoints = []320 context = make_runtime_context()321 bad_fixture = bad_with_setup_error322 with pytest.raises(FixtureSetupError):323 with scoped_context_layer(context):324 the_fixture1 = use_fixture(foo, context, checkpoints, name="foo_1")325 the_fixture2 = use_fixture(bad_fixture, context, checkpoints, name="BAD")326 the_fixture3 = use_fixture(foo, context, checkpoints, name="NOT_REACHED")327 checkpoints.append("scoped-block:NOT_REACHED")328 # -- VERIFY: Ensure cleanup-parts were performed until failure-point.329 assert isinstance(the_fixture1, FooFixture)330 assert the_fixture2 is None # -- NEVER-STORED: Due to setup error.331 assert the_fixture3 is None # -- NEVER-CREATED: Due to bad_fixture.332 assert checkpoints == [333 "foo.setup:foo_1", "bad.setup_with_error", "foo.cleanup:foo_1"]334 def test_bad_with_cleanup_error(self):335 @fixture336 def bad_with_cleanup_error(context, checkpoints, *args, **kwargs):337 checkpoints.append("bad.setup")338 yield FooFixture(*args, **kwargs)339 checkpoints.append("bad.cleanup_with_error")340 raise FixtureCleanupError()341 # -- PERFORM TEST:342 checkpoints = []343 context = make_runtime_context()344 bad_fixture = bad_with_cleanup_error345 with pytest.raises(FixtureCleanupError):346 with scoped_context_layer(context):347 use_fixture(bad_fixture, context, checkpoints)348 checkpoints.append("scoped-block")349 # -- VERIFY: Ensure normal cleanup-parts were performed or tried.350 assert checkpoints == ["bad.setup", "scoped-block", "bad.cleanup_with_error"]351 def test_bad_with_cleanup_error_performs_all_cleanups(self):352 @fixture353 def foo(context, checkpoints, *args, **kwargs):354 fixture_name = kwargs.get("name", "")355 checkpoints.append("foo.setup:%s" % fixture_name)356 yield FooFixture(*args, **kwargs)357 checkpoints.append("foo.cleanup:%s" % fixture_name)358 @fixture359 def bad_with_cleanup_error(context, checkpoints, *args, **kwargs):360 checkpoints.append("bad.setup")361 yield FooFixture(*args, **kwargs)362 checkpoints.append("bad.cleanup_with_error")363 raise FixtureCleanupError()364 checkpoints.append("bad.cleanup.done:NOT_REACHED")365 # -- PERFORM TEST:366 the_fixture1 = None367 the_fixture2 = None368 the_fixture3 = None369 checkpoints = []370 context = make_runtime_context()371 bad_fixture = bad_with_cleanup_error372 with pytest.raises(FixtureCleanupError):373 with scoped_context_layer(context):374 the_fixture1 = use_fixture(foo, context, checkpoints, name="foo_1")375 the_fixture2 = use_fixture(bad_fixture, context, checkpoints, name="BAD")376 the_fixture3 = use_fixture(foo, context, checkpoints, name="foo_3")377 checkpoints.append("scoped-block")378 # -- VERIFY: Tries to perform all cleanups even when cleanup-error(s) occur.379 assert checkpoints == [380 "foo.setup:foo_1", "bad.setup", "foo.setup:foo_3",381 "scoped-block",382 "foo.cleanup:foo_3", "bad.cleanup_with_error", "foo.cleanup:foo_1"]383 def test_bad_with_setup_and_cleanup_error(self):384 # -- GOOD: cleanup_fixture() part is called when setup-error occurs.385 # BUT: FixtureSetupError is hidden by FixtureCleanupError386 @fixture387 def bad_with_setup_and_cleanup_error(context, checkpoints, *args, **kwargs):388 def cleanup_bad_with_error():389 checkpoints.append("bad.cleanup_with_error")390 raise FixtureCleanupError()391 checkpoints.append("bad.setup_with_error")392 context.add_cleanup(cleanup_bad_with_error)393 raise FixtureSetupError()394 return FooFixture("NOT_REACHED")395 # -- PERFORM TEST:396 checkpoints = []397 context = make_runtime_context()398 bad_fixture = bad_with_setup_and_cleanup_error399 with pytest.raises(FixtureCleanupError) as exc_info:400 with scoped_context_layer(context):401 use_fixture(bad_fixture, context, checkpoints, name="BAD2")402 checkpoints.append("scoped-block:NOT_REACHED")403 # -- VERIFY: Ensure normal cleanup-parts were performed or tried.404 # OOPS: FixtureCleanupError in fixture-cleanup hides FixtureSetupError405 assert checkpoints == ["bad.setup_with_error", "bad.cleanup_with_error"]406 assert isinstance(exc_info.value, FixtureCleanupError), "LAST-EXCEPTION-WINS"407class TestUseFixtureByTag(object):408 def test_data_schema1(self):409 @fixture410 def foo(context, *args, **kwargs):411 # -- NOTE checkpoints: Injected from outer scope.412 checkpoints.append("foo.setup")413 yield "fixture:foo"414 checkpoints.append("foo.cleanup")415 fixture_registry = {416 "fixture.foo": foo,417 }418 # -- PERFORM-TEST:419 context = make_runtime_context()420 checkpoints = []421 with scoped_context_layer(context):422 use_fixture_by_tag("fixture.foo", context, fixture_registry)423 checkpoints.append("scoped-block")424 # -- VERIFY:425 assert checkpoints == [426 "foo.setup", "scoped-block", "foo.cleanup"427 ]428 def test_data_schema2(self):429 @fixture430 def foo(context, *args, **kwargs):431 # -- NOTE checkpoints: Injected from outer scope.432 params = "%r, %r" % (args, kwargs)433 checkpoints.append("foo.setup: %s" % params)434 yield "fixture.foo"435 checkpoints.append("foo.cleanup: %s" % params)436 fixture_registry = {437 "fixture.foo": fixture_call_params(foo, 1, 2, 3, name="foo_1")438 }439 # -- PERFORM-TEST:440 context = make_runtime_context()441 checkpoints = []442 with scoped_context_layer(context):443 use_fixture_by_tag("fixture.foo", context, fixture_registry)444 checkpoints.append("scoped-block")445 # -- VERIFY:446 assert checkpoints == [447 "foo.setup: (1, 2, 3), {'name': 'foo_1'}",448 "scoped-block",449 "foo.cleanup: (1, 2, 3), {'name': 'foo_1'}",450 ]451 def test_unknown_fixture_raises_lookup_error(self):452 fixture_registry = {}453 # -- PERFORM-TEST:454 context = make_runtime_context()455 with pytest.raises(LookupError) as exc_info:456 with scoped_context_layer(context):457 use_fixture_by_tag("UNKNOWN_FIXTURE", context, fixture_registry)458 # -- VERIFY:459 assert "Unknown fixture-tag: UNKNOWN_FIXTURE" in str(exc_info.value)460 def test_invalid_data_schema_raises_value_error(self):461 @fixture462 def foo(context, *args, **kwargs):463 pass464 class BadFixtureData(object):465 def __init__(self, fixture_func, *args, **kwargs):466 self.fixture_func = fixture_func467 self.fixture_args = args468 self.fixture_kwargs = kwargs469 fixture_registry = {470 "fixture.foo": BadFixtureData(foo, 1, 2, 3, name="foo_1")471 }472 # -- PERFORM-TEST:473 context = make_runtime_context()474 with pytest.raises(ValueError) as exc_info:475 with scoped_context_layer(context):476 use_fixture_by_tag("fixture.foo", context, fixture_registry)477 # -- VERIFY:478 expected = "fixture_data: Expected tuple or fixture-func, but is:"479 assert expected in str(exc_info.value)480 assert "BadFixtureData object" in str(exc_info.value)481class TestCompositeFixture(object):482 def test_use_fixture(self):483 @fixture484 def fixture_foo(context, checkpoints, *args, **kwargs):485 fixture_name = kwargs.get("name", "foo")486 checkpoints.append("foo.setup:%s" % fixture_name)487 yield488 checkpoints.append("foo.cleanup:%s" % fixture_name)489 @fixture490 def composite2(context, checkpoints, *args, **kwargs):491 the_fixture1 = use_fixture(fixture_foo, context, checkpoints, name="_1")492 the_fixture2 = use_fixture(fixture_foo, context, checkpoints, name="_2")493 return (the_fixture1, the_fixture2)494 # -- PERFORM-TEST:495 context = make_runtime_context()496 checkpoints = []497 with scoped_context_layer(context):498 use_fixture(composite2, context, checkpoints)499 checkpoints.append("scoped-block")500 assert checkpoints == [501 "foo.setup:_1", "foo.setup:_2",502 "scoped-block",503 "foo.cleanup:_2", "foo.cleanup:_1",504 ]505 def test_use_fixture_with_setup_error(self):506 @fixture507 def fixture_foo(context, checkpoints, *args, **kwargs):508 fixture_name = kwargs.get("name", "foo")509 checkpoints.append("foo.setup:%s" % fixture_name)510 yield511 checkpoints.append("foo.cleanup:%s" % fixture_name)512 @fixture513 def bad_with_setup_error(context, checkpoints, *args, **kwargs):514 checkpoints.append("bad.setup_with_error")515 raise FixtureSetupError("OOPS")516 yield517 checkpoints.append("bad.cleanup:NOT_REACHED")518 @fixture519 def composite3(context, checkpoints, *args, **kwargs):520 bad_fixture = bad_with_setup_error521 the_fixture1 = use_fixture(fixture_foo, context, checkpoints, name="_1")522 the_fixture2 = use_fixture(bad_fixture, context, checkpoints, name="OOPS")523 the_fixture3 = use_fixture(fixture_foo, context, checkpoints,524 name="_3:NOT_REACHED")525 return (the_fixture1, the_fixture2, the_fixture3) # NOT_REACHED526 # -- PERFORM-TEST:527 context = make_runtime_context()528 checkpoints = []529 with pytest.raises(FixtureSetupError):530 with scoped_context_layer(context):531 use_fixture(composite3, context, checkpoints)532 checkpoints.append("scoped-block:NOT_REACHED")533 # -- ENSURES:534 # * fixture1-cleanup is called even after fixture2-setup-error535 # * fixture3-setup/cleanup are not called due to fixture2-setup-error536 assert checkpoints == [537 "foo.setup:_1", "bad.setup_with_error", "foo.cleanup:_1"538 ]539 def test_use_fixture_with_block_error(self):540 @fixture541 def fixture_foo(context, checkpoints, *args, **kwargs):542 fixture_name = kwargs.get("name", "foo")543 checkpoints.append("foo.setup:%s" % fixture_name)544 yield545 checkpoints.append("foo.cleanup:%s" % fixture_name)546 @fixture547 def composite2(context, checkpoints, *args, **kwargs):548 the_fixture1 = use_fixture(fixture_foo, context, checkpoints, name="_1")549 the_fixture2 = use_fixture(fixture_foo, context, checkpoints, name="_2")550 return (the_fixture1, the_fixture2)551 # -- PERFORM-TEST:552 context = make_runtime_context()553 checkpoints = []554 with pytest.raises(RuntimeError):555 with scoped_context_layer(context):556 use_fixture(composite2, context, checkpoints)557 checkpoints.append("scoped-block_with_error")558 raise RuntimeError("OOPS")559 checkpoints.append("scoped-block.done:NOT_REACHED")560 # -- ENSURES:561 # * fixture1-cleanup/cleanup is called even scoped-block-error562 # * fixture2-cleanup/cleanup is called even scoped-block-error563 # * fixture-cleanup occurs in reversed setup-order564 assert checkpoints == [565 "foo.setup:_1", "foo.setup:_2",566 "scoped-block_with_error",567 "foo.cleanup:_2", "foo.cleanup:_1"568 ]569 def test_use_composite_fixture(self):570 @fixture571 def fixture_foo(context, checkpoints, *args, **kwargs):572 fixture_name = kwargs.get("name", "foo")573 checkpoints.append("foo.setup:%s" % fixture_name)574 yield575 checkpoints.append("foo.cleanup:%s" % fixture_name)576 @fixture577 def composite2(context, checkpoints, *args, **kwargs):578 the_composite = use_composite_fixture_with(context, [579 fixture_call_params(fixture_foo, checkpoints, name="_1"),580 fixture_call_params(fixture_foo, checkpoints, name="_2"),581 ])582 return the_composite583 # -- PERFORM-TEST:584 context = make_runtime_context()585 checkpoints = []586 with scoped_context_layer(context):587 use_fixture(composite2, context, checkpoints)588 checkpoints.append("scoped-block")589 assert checkpoints == [590 "foo.setup:_1", "foo.setup:_2",591 "scoped-block",592 "foo.cleanup:_2", "foo.cleanup:_1",593 ]594 def test_use_composite_fixture_with_setup_error(self):595 @fixture596 def fixture_foo(context, checkpoints, *args, **kwargs):597 fixture_name = kwargs.get("name", "foo")598 checkpoints.append("foo.setup:%s" % fixture_name)599 yield600 checkpoints.append("foo.cleanup:%s" % fixture_name)601 @fixture602 def bad_with_setup_error(context, checkpoints, *args, **kwargs):603 checkpoints.append("bad.setup_with_error")604 raise FixtureSetupError("OOPS")605 yield606 checkpoints.append("bad.cleanup:NOT_REACHED")607 @fixture608 def composite3(context, checkpoints, *args, **kwargs):609 bad_fixture = bad_with_setup_error610 the_composite = use_composite_fixture_with(context, [611 fixture_call_params(fixture_foo, checkpoints, name="_1"),612 fixture_call_params(bad_fixture, checkpoints, name="OOPS"),613 fixture_call_params(fixture_foo, checkpoints, name="_3:NOT_REACHED"),614 ])615 return the_composite616 # -- PERFORM-TEST:617 context = make_runtime_context()618 checkpoints = []619 with pytest.raises(FixtureSetupError):620 with scoped_context_layer(context):621 use_fixture(composite3, context, checkpoints)622 checkpoints.append("scoped-block:NOT_REACHED")623 # -- ENSURES:624 # * fixture1-cleanup is called even after fixture2-setup-error625 # * fixture3-setup/cleanup are not called due to fixture2-setup-error626 assert checkpoints == [627 "foo.setup:_1", "bad.setup_with_error", "foo.cleanup:_1"628 ]629 def test_use_composite_fixture_with_block_error(self):630 @fixture631 def fixture_foo(context, checkpoints, *args, **kwargs):632 fixture_name = kwargs.get("name", "foo")633 checkpoints.append("foo.setup:%s" % fixture_name)634 yield635 checkpoints.append("foo.cleanup:%s" % fixture_name)636 @fixture637 def composite2(context, checkpoints, *args, **kwargs):638 the_composite = use_composite_fixture_with(context, [639 fixture_call_params(fixture_foo, checkpoints, name="_1"),640 fixture_call_params(fixture_foo, checkpoints, name="_2"),641 ])642 return the_composite643 # -- PERFORM-TEST:644 checkpoints = []645 context = make_runtime_context()646 with pytest.raises(RuntimeError):647 with scoped_context_layer(context):648 use_fixture(composite2, context, checkpoints)649 checkpoints.append("scoped-block_with_error")650 raise RuntimeError("OOPS")651 checkpoints.append("scoped-block.done:NOT_REACHED")652 # -- ENSURES:653 # * fixture1-cleanup/cleanup is called even scoped-block-error654 # * fixture2-cleanup/cleanup is called even scoped-block-error655 # * fixture-cleanup occurs in reversed setup-order656 assert checkpoints == [657 "foo.setup:_1", "foo.setup:_2",658 "scoped-block_with_error",659 "foo.cleanup:_2", "foo.cleanup:_1"660 ]661 # -- BAD-EXAMPLE: Ensure SIMPLISTIC-COMPOSITE works as expected662 def test_simplistic_composite_with_setup_error_skips_cleanup(self):663 def setup_bad_fixture_with_error(text):664 raise FixtureSetupError("OOPS")665 @fixture666 def sad_composite2(context, checkpoints, *args, **kwargs):667 checkpoints.append("foo.setup:_1")668 the_fixture1 = FooFixture.setup(*args, **kwargs)669 checkpoints.append("bad.setup_with_error")670 the_fixture2 = setup_bad_fixture_with_error(text="OOPS")671 checkpoints.append("bad.setup.done:NOT_REACHED")672 yield (the_fixture1, the_fixture2)673 checkpoints.append("foo.cleanup:_1:NOT_REACHED")674 # -- PERFORM-TEST:675 context = make_runtime_context()676 checkpoints = []677 with pytest.raises(FixtureSetupError):678 with scoped_context_layer(context):679 use_fixture(sad_composite2, context, checkpoints)680 checkpoints.append("scoped-block:NOT_REACHED")681 # -- VERIFY:682 # * SAD: fixture1-cleanup is not called after fixture2-setup-error683 assert checkpoints == ["foo.setup:_1", "bad.setup_with_error"]684 def test_simplistic_composite_with_block_error_performs_cleanup(self):685 def setup_bad_fixture_with_error(text):686 raise FixtureSetupError("OOPS")687 @fixture688 def simplistic_composite2(context, checkpoints, *args, **kwargs):689 checkpoints.append("foo.setup:_1")690 the_fixture1 = FooFixture.setup(*args, name="_1")691 checkpoints.append("foo.setup:_2")692 the_fixture2 = FooFixture.setup(*args, name="_2")693 yield (the_fixture1, the_fixture2)694 checkpoints.append("foo.cleanup:_1")695 the_fixture1.cleanup()696 checkpoints.append("foo.cleanup:_2")697 the_fixture2.cleanup()698 # -- PERFORM-TEST:699 context = make_runtime_context()700 checkpoints = []701 with pytest.raises(RuntimeError):702 with scoped_context_layer(context):703 use_fixture(simplistic_composite2, context, checkpoints)704 checkpoints.append("scoped-block_with_error")705 raise RuntimeError("OOPS")706 checkpoints.append("scoped-block.end:NOT_REACHED")707 # -- VERIFY:708 # * fixture1-setup/cleanup is called when block-error occurs709 # * fixture2-setup/cleanup is called when block-error occurs710 # * fixture-cleanups occurs in specified composite-cleanup order711 assert checkpoints == [712 "foo.setup:_1", "foo.setup:_2",713 "scoped-block_with_error",714 "foo.cleanup:_1", "foo.cleanup:_2"715 ]716class TestFixtureCleanup(object):717 """Check fixture-cleanup behaviour w/ different fixture implementations."""718 def test_setup_eror_with_plaingen_then_cleanup_is_not_called(self):719 # -- CASE: Fixture is generator-function720 @fixture721 def foo(context, checkpoints):722 checkpoints.append("foo.setup.begin")723 raise FixtureSetupError("foo")724 checkpoints.append("foo.setup.done:NOT_REACHED")725 yield726 checkpoints.append("foo.cleanup:NOT_REACHED")727 checkpoints = []728 context = make_runtime_context()729 with pytest.raises(FixtureSetupError):730 with scoped_context_layer(context):731 use_fixture(foo, context, checkpoints)732 checkpoints.append("scoped-block:NOT_REACHED")733 assert checkpoints == ["foo.setup.begin"]734 def test_setup_eror_with_finallygen_then_cleanup_is_called(self):735 # -- CASE: Fixture is generator-function736 @fixture737 def foo(context, checkpoints):738 try:739 checkpoints.append("foo.setup.begin")740 raise FixtureSetupError("foo")741 checkpoints.append("foo.setup.done:NOT_REACHED")742 yield743 checkpoints.append("foo.cleanup:NOT_REACHED")744 finally:745 checkpoints.append("foo.cleanup.finally")746 checkpoints = []747 context = make_runtime_context()748 with pytest.raises(FixtureSetupError):749 with scoped_context_layer(context):750 use_fixture(foo, context, checkpoints)751 # -- NEVER-REACHED:752 checkpoints.append("scoped-block:NOT_REACHED")753 # -- ENSURE: fixture-cleanup (foo.cleanup) is called (EARLY-CLEANUP).754 assert checkpoints == ["foo.setup.begin", "foo.cleanup.finally"]755 def test_setup_error_with_context_cleanup1_then_cleanup_is_called(self):756 # -- CASE: Fixture is normal function757 @fixture758 def foo(context, checkpoints):759 def cleanup_foo(arg=""):760 checkpoints.append("cleanup_foo:%s" % arg)761 checkpoints.append("foo.setup_with_error:foo_1")762 context.add_cleanup(cleanup_foo, "foo_1")763 raise FixtureSetupError("foo")764 checkpoints.append("foo.setup.done:NOT_REACHED")765 checkpoints = []766 context = make_runtime_context()767 with pytest.raises(FixtureSetupError):768 with scoped_context_layer(context):769 use_fixture(foo, context, checkpoints)770 checkpoints.append("scoped-block:NOT_REACHED")771 # -- ENSURE: cleanup_foo() is called (LATE-CLEANUP on scope-exit)772 assert checkpoints == ["foo.setup_with_error:foo_1", "cleanup_foo:foo_1"]773 def test_setup_error_with_context_cleanup2_then_cleanup_is_called(self):774 # -- CASE: Fixture is generator-function (contextmanager)775 # NOTE: Explicit use of context.add_cleanup()776 @fixture777 def foo(context, checkpoints, **kwargs):778 def cleanup_foo(arg=""):779 checkpoints.append("cleanup_foo:%s" % arg)780 checkpoints.append("foo.setup_with_error:foo_1")781 context.add_cleanup(cleanup_foo, "foo_1")782 raise FixtureSetupError("foo_1")783 checkpoints.append("foo.setup.done:NOT_REACHED")784 yield785 checkpoints.append("foo.cleanup:NOT_REACHED")786 checkpoints = []787 context = make_runtime_context()788 with pytest.raises(FixtureSetupError):789 with scoped_context_layer(context):790 use_fixture(foo, context, checkpoints)791 checkpoints.append("scoped-block:NOT_REACHED")792 # -- ENSURE: cleanup_foo() is called793 assert checkpoints == ["foo.setup_with_error:foo_1", "cleanup_foo:foo_1"]794 def test_block_eror_with_plaingen_then_cleanup_is_called(self):795 # -- CASE: Fixture is generator-function796 @fixture797 def foo(context, checkpoints):798 checkpoints.append("foo.setup")799 yield800 checkpoints.append("foo.cleanup")801 checkpoints = []802 context = make_runtime_context()803 with pytest.raises(RuntimeError):804 with scoped_context_layer(context):805 use_fixture(foo, context, checkpoints)806 checkpoints.append("scoped-block_with_error")807 raise RuntimeError("scoped-block")808 checkpoints.append("NOT_REACHED")809 # -- ENSURE:810 assert checkpoints == [811 "foo.setup", "scoped-block_with_error", "foo.cleanup"812 ]813 def test_block_eror_with_context_cleanup_then_cleanup_is_called(self):814 # -- CASE: Fixture is normal-function815 @fixture816 def bar(context, checkpoints):817 def cleanup_bar():818 checkpoints.append("cleanup_bar")819 checkpoints.append("bar.setup")820 context.add_cleanup(cleanup_bar)821 checkpoints = []822 context = make_runtime_context()823 with pytest.raises(RuntimeError):824 with scoped_context_layer(context):825 use_fixture(bar, context, checkpoints)826 checkpoints.append("scoped-block_with_error")827 raise RuntimeError("scoped-block")828 checkpoints.append("NOT_REACHED")829 # -- ENSURE:830 assert checkpoints == [831 "bar.setup", "scoped-block_with_error", "cleanup_bar"...

Full Screen

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 Behave 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