How to use scoped_context_layer method in Behave

Best Python code snippet using behave

test_fixture.py

Source:test_fixture.py Github

copy

Full Screen

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

runner.py

Source:runner.py Github

copy

Full Screen

...370 # -- RESTORE: Initial current_mode371 # Even if an AssertionError/Exception is raised.372 context._mode = current_mode373@contextlib.contextmanager374def scoped_context_layer(context, layer_name=None):375 """Provides context manager for context layer (push/do-something/pop cycle).376 .. code-block::377 with scoped_context_layer(context):378 the_fixture = use_fixture(foo, context, name="foo_42")379 """380 # pylint: disable=protected-access381 try:382 context._push(layer_name)383 yield context384 finally:385 context._pop()386def path_getrootdir(path):387 """388 Extract rootdir from path in a platform independent way.389 POSIX-PATH EXAMPLE:390 rootdir = path_getrootdir("/foo/bar/one.feature")391 assert rootdir == "/"...

Full Screen

Full Screen

test_context_cleanups.py

Source:test_context_cleanups.py Github

copy

Full Screen

...37class TestContextCleanup(object):38 def test_cleanup_func_is_called_when_context_frame_is_popped(self):39 my_cleanup = Mock(spec=cleanup_func)40 context = Context(runner=Mock())41 with scoped_context_layer(context): # CALLS-HERE: context._push()42 context.add_cleanup(my_cleanup)43 # -- ENSURE: Not called before context._pop()44 my_cleanup.assert_not_called()45 # CALLS-HERE: context._pop()46 my_cleanup.assert_called_once()47 # MAYBE: my_cleanup2.assert_called_with(context)48 def test_cleanup_funcs_are_called_when_context_frame_is_popped(self):49 my_cleanup1 = Mock(spec=cleanup_func)50 my_cleanup2 = Mock(spec=cleanup_func)51 # -- SETUP:52 context = Context(runner=Mock())53 with scoped_context_layer(context):54 context.add_cleanup(my_cleanup1)55 context.add_cleanup(my_cleanup2)56 # -- ENSURE: Not called before context._pop()57 my_cleanup1.assert_not_called()58 my_cleanup2.assert_not_called()59 # -- CALLS-HERE: context._pop()60 my_cleanup1.assert_called_once()61 my_cleanup2.assert_called_once()62 def test_cleanup_funcs_are_called_in_reversed_order(self):63 call_listener = CallListener()64 my_cleanup1A = CleanupFunction("CLEANUP1", listener=call_listener)65 my_cleanup2A = CleanupFunction("CLEANUP2", listener=call_listener)66 my_cleanup1 = Mock(side_effect=my_cleanup1A)67 my_cleanup2 = Mock(side_effect=my_cleanup2A)68 # -- SETUP:69 context = Context(runner=Mock())70 with scoped_context_layer(context):71 context.add_cleanup(my_cleanup1)72 context.add_cleanup(my_cleanup2)73 my_cleanup1.assert_not_called()74 my_cleanup2.assert_not_called()75 # -- ENSURE: Reversed order of cleanup calls76 expected_call_order = ["called:CLEANUP2", "called:CLEANUP1"]77 assert call_listener.collected == expected_call_order78 my_cleanup1.assert_called_once()79 my_cleanup2.assert_called_once()80 def test_cleanup_funcs_on_two_context_frames(self):81 call_listener = CallListener()82 my_cleanup_A1 = CleanupFunction("CLEANUP_A1", listener=call_listener)83 my_cleanup_A2 = CleanupFunction("CLEANUP_A2", listener=call_listener)84 my_cleanup_B1 = CleanupFunction("CLEANUP_B1", listener=call_listener)85 my_cleanup_B2 = CleanupFunction("CLEANUP_B2", listener=call_listener)86 my_cleanup_B3 = CleanupFunction("CLEANUP_B3", listener=call_listener)87 my_cleanup_A1M = Mock(side_effect=my_cleanup_A1)88 my_cleanup_A2M = Mock(side_effect=my_cleanup_A2)89 my_cleanup_B1M = Mock(side_effect=my_cleanup_B1)90 my_cleanup_B2M = Mock(side_effect=my_cleanup_B2)91 my_cleanup_B3M = Mock(side_effect=my_cleanup_B3)92 # -- SETUP:93 context = Context(runner=Mock())94 with scoped_context_layer(context): # -- LAYER A:95 context.add_cleanup(my_cleanup_A1M)96 context.add_cleanup(my_cleanup_A2M)97 with scoped_context_layer(context): # -- LAYER B:98 context.add_cleanup(my_cleanup_B1M)99 context.add_cleanup(my_cleanup_B2M)100 context.add_cleanup(my_cleanup_B3M)101 my_cleanup_B1M.assert_not_called()102 my_cleanup_B2M.assert_not_called()103 my_cleanup_B3M.assert_not_called()104 # -- context.pop(LAYER_B): Call cleanups for Bx105 expected_call_order = [106 "called:CLEANUP_B3", "called:CLEANUP_B2", "called:CLEANUP_B1",107 ]108 assert call_listener.collected == expected_call_order109 my_cleanup_A1M.assert_not_called()110 my_cleanup_A2M.assert_not_called()111 my_cleanup_B1M.assert_called_once()112 my_cleanup_B2M.assert_called_once()113 my_cleanup_B3M.assert_called_once()114 # -- context.pop(LAYER_A): Call cleanups for Ax115 expected_call_order = [116 "called:CLEANUP_B3", "called:CLEANUP_B2", "called:CLEANUP_B1",117 "called:CLEANUP_A2", "called:CLEANUP_A1",118 ]119 assert call_listener.collected == expected_call_order120 my_cleanup_A1M.assert_called_once()121 my_cleanup_A2M.assert_called_once()122 my_cleanup_B1M.assert_called_once()123 my_cleanup_B2M.assert_called_once()124 my_cleanup_B3M.assert_called_once()125 def test_add_cleanup__rejects_noncallable_cleanup_func(self):126 class NonCallable(object): pass127 non_callable = NonCallable()128 context = Context(runner=Mock())129 with pytest.raises(AssertionError) as e:130 with scoped_context_layer(context):131 context.add_cleanup(non_callable)132 assert "REQUIRES: callable(cleanup_func)" in str(e)133 def test_on_cleanup_error__prints_error_by_default(self, capsys):134 def bad_cleanup_func():135 raise RuntimeError("in CLEANUP call")136 bad_cleanup = Mock(side_effect=bad_cleanup_func)137 context = Context(runner=Mock())138 with pytest.raises(RuntimeError):139 with scoped_context_layer(context):140 context.add_cleanup(bad_cleanup)141 captured_output, _ = capsys.readouterr()142 bad_cleanup.assert_called()143 assert "CLEANUP-ERROR in " in captured_output144 assert "RuntimeError: in CLEANUP call" in captured_output145 # -- FOR DIAGNOSTICS:146 print(captured_output)147 def test_on_cleanup_error__is_called_if_defined(self):148 def bad_cleanup():149 raise RuntimeError("in CLEANUP call")150 def handle_cleanup_error(context, cleanup_func, exception):151 print("CALLED: handle_cleanup_error")152 context = Context(runner=Mock())153 handle_cleanup_error_func = Mock(spec=handle_cleanup_error)154 with pytest.raises(RuntimeError):155 with scoped_context_layer(context):156 context.on_cleanup_error = handle_cleanup_error_func157 context.add_cleanup(bad_cleanup)158 handle_cleanup_error_func.assert_called_once()159 # expected_args = (context, handle_cleanup_error_func,160 # RuntimeError("in CLEANUP call"))161 # handle_cleanup_error_func.assert_called_with(*expected_args)162 def test_on_cleanup_error__may_be_called_several_times_per_cleanup(self):163 def bad_cleanup1():164 raise RuntimeError("CLEANUP_1")165 def bad_cleanup2():166 raise RuntimeError("CLEANUP_2")167 class CleanupErrorCollector(object):168 def __init__(self):169 self.collected = []170 def __call__(self, context, cleanup_func, exception):171 self.collected.append((context, cleanup_func, exception))172 context = Context(runner=Mock())173 collect_cleanup_error = CleanupErrorCollector()174 with pytest.raises(RuntimeError):175 with scoped_context_layer(context):176 context.on_cleanup_error = collect_cleanup_error177 context.add_cleanup(bad_cleanup1)178 context.add_cleanup(bad_cleanup2)179 expected = [180 (context, bad_cleanup2, RuntimeError("CLEANUP_2")),181 (context, bad_cleanup1, RuntimeError("CLEANUP_1")),182 ]183 assert len(collect_cleanup_error.collected) == 2184 assert collect_cleanup_error.collected[0][:-1] == expected[0][:-1]...

Full Screen

Full Screen

runner.pyi

Source:runner.pyi Github

copy

Full Screen

...22Mode = Union[Literal["behave"], Literal["user"]]23@contextlib.contextmanager24def use_context_with_mode(context: Context, mode: Mode) -> Iterator[None]: ...25@contextlib.contextmanager26def scoped_context_layer(context: Context, layer_name: str|None = None) -> Iterator[Context]: ...27def path_getrootdir(path: str)-> str: ...28class CleanupError(RuntimeError): ...29class ContextMaskWarning(UserWarning): ...30class Context(Protocol):31 def __getattr__(self, name: str) -> Any: ...32 def __setattr__(self, name: str, value: Any) -> None: ...33 def __contains__(self, name: str) -> bool: ...34 def add_cleanup(self, cleanup_func: Callable[..., None], *a: Any, **k: Any) -> None: ...35 @property36 def config(self) -> Configuration: ...37 @property38 def aborted(self) -> bool: ...39 @property40 def failed(self) -> bool: ......

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