def test_bad_with_cleanup_error_performs_all_cleanups(self): @fixture def foo(context, checkpoints, *args, **kwargs): fixture_name = kwargs.get("name", "") checkpoints.append("foo.setup:%s" % fixture_name) yield FooFixture(*args, **kwargs) checkpoints.append("foo.cleanup:%s" % fixture_name) @fixture def bad_with_cleanup_error(context, checkpoints, *args, **kwargs): checkpoints.append("bad.setup") yield FooFixture(*args, **kwargs) checkpoints.append("bad.cleanup_with_error") raise FixtureCleanupError() checkpoints.append("bad.cleanup.done:NOT_REACHED") # -- PERFORM TEST: the_fixture1 = None the_fixture2 = None the_fixture3 = None checkpoints = [] context = make_runtime_context() bad_fixture = bad_with_cleanup_error with pytest.raises(FixtureCleanupError): with scoped_context_layer(context): the_fixture1 = use_fixture(foo, context, checkpoints, name="foo_1") the_fixture2 = use_fixture(bad_fixture, context, checkpoints, name="BAD") the_fixture3 = use_fixture(foo, context, checkpoints, name="foo_3") checkpoints.append("scoped-block") # -- VERIFY: Tries to perform all cleanups even when cleanup-error(s) occur. assert checkpoints == [ "foo.setup:foo_1", "bad.setup", "foo.setup:foo_3", "scoped-block", "foo.cleanup:foo_3", "bad.cleanup_with_error", "foo.cleanup:foo_1"]
def test_use_composite_fixture_with_block_error(self): @fixture def fixture_foo(context, checkpoints, *args, **kwargs): 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_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_simplistic_composite_with_block_error_performs_cleanup(self): def setup_bad_fixture_with_error(text): raise FixtureSetupError("OOPS") @fixture def simplistic_composite2(context, checkpoints, *args, **kwargs): checkpoints.append("foo.setup:_1") the_fixture1 = FooFixture.setup(*args, name="_1") checkpoints.append("foo.setup:_2") the_fixture2 = FooFixture.setup(*args, name="_2") yield (the_fixture1, the_fixture2) checkpoints.append("foo.cleanup:_1") the_fixture1.cleanup() checkpoints.append("foo.cleanup:_2") the_fixture2.cleanup() # -- PERFORM-TEST: context = make_runtime_context() checkpoints = [] with pytest.raises(RuntimeError): with scoped_context_layer(context): use_fixture(simplistic_composite2, context, checkpoints) checkpoints.append("scoped-block_with_error") raise RuntimeError("OOPS") checkpoints.append("scoped-block.end:NOT_REACHED") # -- VERIFY: # * fixture1-setup/cleanup is called when block-error occurs # * fixture2-setup/cleanup is called when block-error occurs # * fixture-cleanups occurs in specified composite-cleanup order assert checkpoints == [ "foo.setup:_1", "foo.setup:_2", "scoped-block_with_error", "foo.cleanup:_1", "foo.cleanup:_2" ]
def test_can_use_fixture_two_times(self): """Ensures that a fixture can be used multiple times (with different names) within a context layer. """ @fixture def foo(context, checkpoints, *args, **kwargs): fixture_object = FooFixture.setup(*args, **kwargs) setattr(context, fixture_object.name, fixture_object) checkpoints.append("foo.setup:%s" % fixture_object.name) yield fixture_object checkpoints.append("foo.cleanup:%s" % fixture_object.name) fixture_object.cleanup() checkpoints = [] context = make_runtime_context() with scoped_context_layer(context): the_fixture1 = use_fixture(foo, context, checkpoints, name="foo_1") the_fixture2 = use_fixture(foo, context, checkpoints, name="foo_2") # -- VERIFY: Fixture and context setup was performed. assert checkpoints == ["foo.setup:foo_1", "foo.setup:foo_2"] assert context.foo_1 is the_fixture1 assert context.foo_2 is the_fixture2 assert the_fixture1 is not the_fixture2 checkpoints.append("scoped-block") # -- VERIFY: Fixture and context cleanup is performed. assert_context_cleanup(context, "foo_1") assert_context_cleanup(context, "foo_2") assert checkpoints == [ "foo.setup:foo_1", "foo.setup:foo_2", "scoped-block", "foo.cleanup:foo_2", "foo.cleanup:foo_1" ]
def test_simplistic_composite_with_setup_error_skips_cleanup(self): def setup_bad_fixture_with_error(text): raise FixtureSetupError("OOPS") @fixture def sad_composite2(context, checkpoints, *args, **kwargs): checkpoints.append("foo.setup:_1") the_fixture1 = FooFixture.setup(*args, **kwargs) checkpoints.append("bad.setup_with_error") the_fixture2 = setup_bad_fixture_with_error(text="OOPS") checkpoints.append("bad.setup.done:NOT_REACHED") yield (the_fixture1, the_fixture2) checkpoints.append("foo.cleanup:_1:NOT_REACHED") # -- PERFORM-TEST: context = make_runtime_context() checkpoints = [] with pytest.raises(FixtureSetupError): with scoped_context_layer(context): use_fixture(sad_composite2, context, checkpoints) checkpoints.append("scoped-block:NOT_REACHED") # -- VERIFY: # * SAD: fixture1-cleanup is not called after fixture2-setup-error assert checkpoints == ["foo.setup:_1", "bad.setup_with_error"]
def composite3(context, checkpoints, *args, **kwargs): bad_fixture = bad_with_setup_error the_fixture1 = use_fixture(fixture_foo, context, checkpoints, name="_1") the_fixture2 = use_fixture(bad_fixture, context, checkpoints, name="OOPS") the_fixture3 = use_fixture(fixture_foo, context, checkpoints, name="_3:NOT_REACHED") return (the_fixture1, the_fixture2, the_fixture3) # NOT_REACHED
def test_bad_with_setup_and_cleanup_error(self): # -- GOOD: cleanup_fixture() part is called when setup-error occurs. # BUT: FixtureSetupError is hidden by FixtureCleanupError @fixture def bad_with_setup_and_cleanup_error(context, checkpoints, *args, **kwargs): def cleanup_bad_with_error(): checkpoints.append("bad.cleanup_with_error") raise FixtureCleanupError() checkpoints.append("bad.setup_with_error") context.add_cleanup(cleanup_bad_with_error) raise FixtureSetupError() return FooFixture("NOT_REACHED") # -- PERFORM TEST: checkpoints = [] context = make_runtime_context() bad_fixture = bad_with_setup_and_cleanup_error with pytest.raises(FixtureCleanupError) as exc_info: with scoped_context_layer(context): use_fixture(bad_fixture, context, checkpoints, name="BAD2") checkpoints.append("scoped-block:NOT_REACHED") # -- VERIFY: Ensure normal cleanup-parts were performed or tried. # OOPS: FixtureCleanupError in fixture-cleanup hides FixtureSetupError assert checkpoints == ["bad.setup_with_error", "bad.cleanup_with_error"] assert isinstance(exc_info.value, FixtureCleanupError), "LAST-EXCEPTION-WINS"
def test_use_composite_fixture(self): @fixture def fixture_foo(context, checkpoints, *args, **kwargs): 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_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: context = make_runtime_context() checkpoints = [] with scoped_context_layer(context): use_fixture(composite2, context, checkpoints) checkpoints.append("scoped-block") assert checkpoints == [ "foo.setup:_1", "foo.setup:_2", "scoped-block", "foo.cleanup:_2", "foo.cleanup:_1", ]
def test_use_composite_fixture_with_block_error(self): @fixture def fixture_foo(context, checkpoints, *args, **kwargs): 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_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_simplistic_composite_with_setup_error_skips_cleanup(self): def setup_bad_fixture_with_error(text): raise FixtureSetupError("OOPS") @fixture def sad_composite2(context, checkpoints, *args, **kwargs): checkpoints.append("foo.setup:_1") the_fixture1 = FooFixture.setup(*args, **kwargs) checkpoints.append("bad.setup_with_error") the_fixture2 = setup_bad_fixture_with_error(text="OOPS") checkpoints.append("bad.setup.done:NOT_REACHED") yield (the_fixture1, the_fixture2) checkpoints.append("foo.cleanup:_1:NOT_REACHED") # -- PERFORM-TEST: context = make_runtime_context() checkpoints = [] with pytest.raises(FixtureSetupError): with scoped_context_layer(context): use_fixture(sad_composite2, context, checkpoints) checkpoints.append("scoped-block:NOT_REACHED") # -- VERIFY: # * SAD: fixture1-cleanup is not called after fixture2-setup-error assert checkpoints == ["foo.setup:_1", "bad.setup_with_error"]
def test_can_use_fixture_two_times(self): """Ensures that a fixture can be used multiple times (with different names) within a context layer. """ @fixture def foo(context, checkpoints, *args, **kwargs): fixture_object = FooFixture.setup(*args, **kwargs) setattr(context, fixture_object.name, fixture_object) checkpoints.append("foo.setup:%s" % fixture_object.name) yield fixture_object checkpoints.append("foo.cleanup:%s" % fixture_object.name) fixture_object.cleanup() checkpoints = [] context = make_runtime_context() with scoped_context_layer(context): the_fixture1 = use_fixture(foo, context, checkpoints, name="foo_1") the_fixture2 = use_fixture(foo, context, checkpoints, name="foo_2") # -- VERIFY: Fixture and context setup was performed. assert checkpoints == ["foo.setup:foo_1", "foo.setup:foo_2"] assert context.foo_1 is the_fixture1 assert context.foo_2 is the_fixture2 assert the_fixture1 is not the_fixture2 checkpoints.append("scoped-block") # -- VERIFY: Fixture and context cleanup is performed. assert_context_cleanup(context, "foo_1") assert_context_cleanup(context, "foo_2") assert checkpoints == ["foo.setup:foo_1", "foo.setup:foo_2", "scoped-block", "foo.cleanup:foo_2", "foo.cleanup:foo_1"]
def test_bad_with_setup_error_aborts_on_first_error(self): @fixture def foo(context, checkpoints, *args, **kwargs): fixture_name = kwargs.get("name", "") checkpoints.append("foo.setup:%s" % fixture_name) yield FooFixture(*args, **kwargs) checkpoints.append("foo.cleanup:%s" % fixture_name) @fixture def bad_with_setup_error(context, checkpoints, *args, **kwargs): checkpoints.append("bad.setup_with_error") raise FixtureSetupError() yield FooFixture(*args, **kwargs) checkpoints.append("bad.cleanup:NOT_REACHED") # -- PERFORM-TEST: the_fixture1 = None the_fixture2 = None the_fixture3 = None checkpoints = [] context = make_runtime_context() bad_fixture = bad_with_setup_error with pytest.raises(FixtureSetupError): with scoped_context_layer(context): the_fixture1 = use_fixture(foo, context, checkpoints, name="foo_1") the_fixture2 = use_fixture(bad_fixture, context, checkpoints, name="BAD") the_fixture3 = use_fixture(foo, context, checkpoints, name="NOT_REACHED") checkpoints.append("scoped-block:NOT_REACHED") # -- VERIFY: Ensure cleanup-parts were performed until failure-point. assert isinstance(the_fixture1, FooFixture) assert the_fixture2 is None # -- NEVER-STORED: Due to setup error. assert the_fixture3 is None # -- NEVER-CREATED: Due to bad_fixture. assert checkpoints == [ "foo.setup:foo_1", "bad.setup_with_error", "foo.cleanup:foo_1"]
def before_scenario(context, scenario): tags = scenario.feature.tags if "fixture.browser.chrome" in tags: use_fixture(selenium_browser_chrome, context) if "fixture.browser.firefox" in tags: use_fixture(selenium_browser_firefox, context) context.browser.maximize_window()
def test_simplistic_composite_with_block_error_performs_cleanup(self): def setup_bad_fixture_with_error(text): raise FixtureSetupError("OOPS") @fixture def simplistic_composite2(context, checkpoints, *args, **kwargs): checkpoints.append("foo.setup:_1") the_fixture1 = FooFixture.setup(*args, name="_1") checkpoints.append("foo.setup:_2") the_fixture2 = FooFixture.setup(*args, name="_2") yield (the_fixture1, the_fixture2) checkpoints.append("foo.cleanup:_1") the_fixture1.cleanup() checkpoints.append("foo.cleanup:_2") the_fixture2.cleanup() # -- PERFORM-TEST: context = make_runtime_context() checkpoints = [] with pytest.raises(RuntimeError): with scoped_context_layer(context): use_fixture(simplistic_composite2, context, checkpoints) checkpoints.append("scoped-block_with_error") raise RuntimeError("OOPS") checkpoints.append("scoped-block.end:NOT_REACHED") # -- VERIFY: # * fixture1-setup/cleanup is called when block-error occurs # * fixture2-setup/cleanup is called when block-error occurs # * fixture-cleanups occurs in specified composite-cleanup order assert checkpoints == [ "foo.setup:_1", "foo.setup:_2", "scoped-block_with_error", "foo.cleanup:_1", "foo.cleanup:_2" ]
def test_use_composite_fixture(self): @fixture def fixture_foo(context, checkpoints, *args, **kwargs): 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_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: context = make_runtime_context() checkpoints = [] with scoped_context_layer(context): use_fixture(composite2, context, checkpoints) checkpoints.append("scoped-block") assert checkpoints == [ "foo.setup:_1", "foo.setup:_2", "scoped-block", "foo.cleanup:_2", "foo.cleanup:_1", ]
def test_bad_with_setup_and_cleanup_error(self): # -- GOOD: cleanup_fixture() part is called when setup-error occurs. # BUT: FixtureSetupError is hidden by FixtureCleanupError @fixture def bad_with_setup_and_cleanup_error(context, checkpoints, *args, **kwargs): def cleanup_bad_with_error(): checkpoints.append("bad.cleanup_with_error") raise FixtureCleanupError() checkpoints.append("bad.setup_with_error") context.add_cleanup(cleanup_bad_with_error) raise FixtureSetupError() return FooFixture("NOT_REACHED") # -- PERFORM TEST: checkpoints = [] context = make_runtime_context() bad_fixture = bad_with_setup_and_cleanup_error with pytest.raises(FixtureCleanupError) as exc_info: with scoped_context_layer(context): use_fixture(bad_fixture, context, checkpoints, name="BAD2") checkpoints.append("scoped-block:NOT_REACHED") # -- VERIFY: Ensure normal cleanup-parts were performed or tried. # OOPS: FixtureCleanupError in fixture-cleanup hides FixtureSetupError assert checkpoints == [ "bad.setup_with_error", "bad.cleanup_with_error" ] assert isinstance(exc_info.value, FixtureCleanupError), "LAST-EXCEPTION-WINS"
def test_setup_error_with_context_cleanup2_then_cleanup_is_called(self): # -- CASE: Fixture is generator-function (contextmanager) # NOTE: Explicit use of context.add_cleanup() @fixture def foo(context, checkpoints, **kwargs): 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_1") checkpoints.append("foo.setup.done:NOT_REACHED") yield checkpoints.append("foo.cleanup: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 assert checkpoints == [ "foo.setup_with_error:foo_1", "cleanup_foo:foo_1" ]
def before_all(context): driver, kwargs = get_driver() context.BehaveDriver = partial(driver, **kwargs) use_fixture(fixture_browser, context, webdriver=driver, **kwargs) use_fixture(transformation_fixture, context, FormatTransformer, BASE_URL='http://localhost:8000', ALT_BASE_URL='http://127.0.0.1:8000')
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)
def before_all(context): parser = JsonParser() params = parser.parse("config.json") app_package = params['AppPackage'] context.phoneNumber = params['PhoneNumber'] context.password = params['Password'] use_fixture(setup_driver, context) subprocess.call("adb shell pm grant " + app_package + " android.permission.ACCESS_COARSE_LOCATION", shell=False)
def composite3(context, checkpoints, *args, **kwargs): bad_fixture = bad_with_setup_error the_fixture1 = use_fixture(fixture_foo, context, checkpoints, name="_1") the_fixture2 = use_fixture(bad_fixture, context, checkpoints, name="OOPS") the_fixture3 = use_fixture(fixture_foo, context, checkpoints, name="_3:NOT_REACHED") return (the_fixture1, the_fixture2, the_fixture3) # NOT_REACHED
def use_fixture_by_tag(tag, context, fixture_registry): fixture_data = fixture_registry.get(tag, None) if fixture_data is None: raise LookupError('Unknown fixture-tag: %s' % tag) fixture_func, fixture_args, fixture_kwargs = fixture_data return use_fixture(fixture_func, context, *fixture_args, **fixture_kwargs)
def test_use_fixture_with_setup_error(self): @fixture def fixture_foo(context, checkpoints, *args, **kwargs): fixture_name = kwargs.get("name", "foo") checkpoints.append("foo.setup:%s" % fixture_name) yield checkpoints.append("foo.cleanup:%s" % fixture_name) @fixture def bad_with_setup_error(context, checkpoints, *args, **kwargs): checkpoints.append("bad.setup_with_error") raise FixtureSetupError("OOPS") yield checkpoints.append("bad.cleanup:NOT_REACHED") @fixture def composite3(context, checkpoints, *args, **kwargs): bad_fixture = bad_with_setup_error the_fixture1 = use_fixture(fixture_foo, context, checkpoints, name="_1") the_fixture2 = use_fixture(bad_fixture, context, checkpoints, name="OOPS") the_fixture3 = use_fixture(fixture_foo, context, checkpoints, name="_3:NOT_REACHED") return (the_fixture1, the_fixture2, the_fixture3) # NOT_REACHED # -- PERFORM-TEST: context = make_runtime_context() checkpoints = [] with pytest.raises(FixtureSetupError): with scoped_context_layer(context): use_fixture(composite3, context, checkpoints) checkpoints.append("scoped-block:NOT_REACHED") # -- ENSURES: # * fixture1-cleanup is called even after fixture2-setup-error # * fixture3-setup/cleanup are not called due to fixture2-setup-error assert checkpoints == [ "foo.setup:_1", "bad.setup_with_error", "foo.cleanup:_1" ]
def test_bad_with_setup_error_aborts_on_first_error(self): @fixture def foo(context, checkpoints, *args, **kwargs): fixture_name = kwargs.get("name", "") checkpoints.append("foo.setup:%s" % fixture_name) yield FooFixture(*args, **kwargs) checkpoints.append("foo.cleanup:%s" % fixture_name) @fixture def bad_with_setup_error(context, checkpoints, *args, **kwargs): checkpoints.append("bad.setup_with_error") raise FixtureSetupError() yield FooFixture(*args, **kwargs) checkpoints.append("bad.cleanup:NOT_REACHED") # -- PERFORM-TEST: the_fixture1 = None the_fixture2 = None the_fixture3 = None checkpoints = [] context = make_runtime_context() bad_fixture = bad_with_setup_error with pytest.raises(FixtureSetupError): with scoped_context_layer(context): the_fixture1 = use_fixture(foo, context, checkpoints, name="foo_1") the_fixture2 = use_fixture(bad_fixture, context, checkpoints, name="BAD") the_fixture3 = use_fixture(foo, context, checkpoints, name="NOT_REACHED") checkpoints.append("scoped-block:NOT_REACHED") # -- VERIFY: Ensure cleanup-parts were performed until failure-point. assert isinstance(the_fixture1, FooFixture) assert the_fixture2 is None # -- NEVER-STORED: Due to setup error. assert the_fixture3 is None # -- NEVER-CREATED: Due to bad_fixture. assert checkpoints == [ "foo.setup:foo_1", "bad.setup_with_error", "foo.cleanup:foo_1" ]
def test_setup_eror_with_plaingen_then_cleanup_is_not_called(self): # -- CASE: Fixture is generator-function @fixture def foo(context, checkpoints): checkpoints.append("foo.setup.begin") raise FixtureSetupError("foo") checkpoints.append("foo.setup.done:NOT_REACHED") yield checkpoints.append("foo.cleanup: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") assert checkpoints == ["foo.setup.begin"]
def test_setup_eror_with_plaingen_then_cleanup_is_not_called(self): # -- CASE: Fixture is generator-function @fixture def foo(context, checkpoints): checkpoints.append("foo.setup.begin") raise FixtureSetupError("foo") checkpoints.append("foo.setup.done:NOT_REACHED") yield checkpoints.append("foo.cleanup: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") assert checkpoints == ["foo.setup.begin"]
def test_bad_with_cleanup_error_performs_all_cleanups(self): @fixture def foo(context, checkpoints, *args, **kwargs): fixture_name = kwargs.get("name", "") checkpoints.append("foo.setup:%s" % fixture_name) yield FooFixture(*args, **kwargs) checkpoints.append("foo.cleanup:%s" % fixture_name) @fixture def bad_with_cleanup_error(context, checkpoints, *args, **kwargs): checkpoints.append("bad.setup") yield FooFixture(*args, **kwargs) checkpoints.append("bad.cleanup_with_error") raise FixtureCleanupError() checkpoints.append("bad.cleanup.done:NOT_REACHED") # -- PERFORM TEST: the_fixture1 = None the_fixture2 = None the_fixture3 = None checkpoints = [] context = make_runtime_context() bad_fixture = bad_with_cleanup_error with pytest.raises(FixtureCleanupError): with scoped_context_layer(context): the_fixture1 = use_fixture(foo, context, checkpoints, name="foo_1") the_fixture2 = use_fixture(bad_fixture, context, checkpoints, name="BAD") the_fixture3 = use_fixture(foo, context, checkpoints, name="foo_3") checkpoints.append("scoped-block") # -- VERIFY: Tries to perform all cleanups even when cleanup-error(s) occur. assert checkpoints == [ "foo.setup:foo_1", "bad.setup", "foo.setup:foo_3", "scoped-block", "foo.cleanup:foo_3", "bad.cleanup_with_error", "foo.cleanup:foo_1" ]
def test_bad_with_cleanup_error(self): @fixture def bad_with_cleanup_error(context, checkpoints, *args, **kwargs): checkpoints.append("bad.setup") yield FooFixture(*args, **kwargs) checkpoints.append("bad.cleanup_with_error") raise FixtureCleanupError() # -- PERFORM TEST: checkpoints = [] context = make_runtime_context() bad_fixture = bad_with_cleanup_error with pytest.raises(FixtureCleanupError): with scoped_context_layer(context): use_fixture(bad_fixture, context, checkpoints) checkpoints.append("scoped-block") # -- VERIFY: Ensure normal cleanup-parts were performed or tried. assert checkpoints == ["bad.setup", "scoped-block", "bad.cleanup_with_error"]
def before_tag(context, tag): if tag == 'fixture.platform.all': context.chrome_driver = use_fixture(init_chrome_driver, context, timeout=10) context.android_driver = use_fixture(init_android_driver, context, timeout=10) context.platform = Platform.All.value elif tag == 'CF': context.platform = Platform.Chrome.value context.pu = tag elif tag == 'beep.delivery': context.platform = Platform.Chrome.value context.pu = tag elif tag == 'fixture.platform.safari': context.chrome_driver = use_fixture( init_safari_driver_mobile_emulation_beep_delivery, context, timeout=10)
def test_block_eror_with_plaingen_then_cleanup_is_called(self): # -- CASE: Fixture is generator-function @fixture def foo(context, checkpoints): checkpoints.append("foo.setup") yield checkpoints.append("foo.cleanup") checkpoints = [] context = make_runtime_context() with pytest.raises(RuntimeError): with scoped_context_layer(context): use_fixture(foo, context, checkpoints) checkpoints.append("scoped-block_with_error") raise RuntimeError("scoped-block") checkpoints.append("NOT_REACHED") # -- ENSURE: assert checkpoints == [ "foo.setup", "scoped-block_with_error", "foo.cleanup" ]
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"]
def test_block_eror_with_plaingen_then_cleanup_is_called(self): # -- CASE: Fixture is generator-function @fixture def foo(context, checkpoints): checkpoints.append("foo.setup") yield checkpoints.append("foo.cleanup") checkpoints = [] context = make_runtime_context() with pytest.raises(RuntimeError): with scoped_context_layer(context): use_fixture(foo, context, checkpoints) checkpoints.append("scoped-block_with_error") raise RuntimeError("scoped-block") checkpoints.append("NOT_REACHED") # -- ENSURE: assert checkpoints == [ "foo.setup", "scoped-block_with_error", "foo.cleanup" ]
def test_bad_with_cleanup_error(self): @fixture def bad_with_cleanup_error(context, checkpoints, *args, **kwargs): checkpoints.append("bad.setup") yield FooFixture(*args, **kwargs) checkpoints.append("bad.cleanup_with_error") raise FixtureCleanupError() # -- PERFORM TEST: checkpoints = [] context = make_runtime_context() bad_fixture = bad_with_cleanup_error with pytest.raises(FixtureCleanupError): with scoped_context_layer(context): use_fixture(bad_fixture, context, checkpoints) checkpoints.append("scoped-block") # -- VERIFY: Ensure normal cleanup-parts were performed or tried. assert checkpoints == [ "bad.setup", "scoped-block", "bad.cleanup_with_error" ]
def test_use_composite_fixture_with_setup_error(self): @fixture def fixture_foo(context, checkpoints, *args, **kwargs): fixture_name = kwargs.get("name", "foo") checkpoints.append("foo.setup:%s" % fixture_name) yield checkpoints.append("foo.cleanup:%s" % fixture_name) @fixture def bad_with_setup_error(context, checkpoints, *args, **kwargs): checkpoints.append("bad.setup_with_error") raise FixtureSetupError("OOPS") yield checkpoints.append("bad.cleanup:NOT_REACHED") @fixture def composite3(context, checkpoints, *args, **kwargs): bad_fixture = bad_with_setup_error the_composite = use_composite_fixture_with(context, [ fixture_call_params(fixture_foo, checkpoints, name="_1"), fixture_call_params(bad_fixture, checkpoints, name="OOPS"), fixture_call_params(fixture_foo, checkpoints, name="_3:NOT_REACHED"), ]) return the_composite # -- PERFORM-TEST: context = make_runtime_context() checkpoints = [] with pytest.raises(FixtureSetupError): with scoped_context_layer(context): use_fixture(composite3, context, checkpoints) checkpoints.append("scoped-block:NOT_REACHED") # -- ENSURES: # * fixture1-cleanup is called even after fixture2-setup-error # * fixture3-setup/cleanup are not called due to fixture2-setup-error assert checkpoints == [ "foo.setup:_1", "bad.setup_with_error", "foo.cleanup:_1" ]
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
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" ]
def test_setup_eror_with_finallygen_then_cleanup_is_called(self): # -- CASE: Fixture is generator-function @fixture def foo(context, checkpoints): try: checkpoints.append("foo.setup.begin") raise FixtureSetupError("foo") checkpoints.append("foo.setup.done:NOT_REACHED") yield checkpoints.append("foo.cleanup:NOT_REACHED") finally: checkpoints.append("foo.cleanup.finally") checkpoints = [] context = make_runtime_context() with pytest.raises(FixtureSetupError): with scoped_context_layer(context): use_fixture(foo, context, checkpoints) # -- NEVER-REACHED: checkpoints.append("scoped-block:NOT_REACHED") # -- ENSURE: fixture-cleanup (foo.cleanup) is called (EARLY-CLEANUP). assert checkpoints == ["foo.setup.begin", "foo.cleanup.finally"]
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_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_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
def test_setup_eror_with_finallygen_then_cleanup_is_called(self): # -- CASE: Fixture is generator-function @fixture def foo(context, checkpoints): try: checkpoints.append("foo.setup.begin") raise FixtureSetupError("foo") checkpoints.append("foo.setup.done:NOT_REACHED") yield checkpoints.append("foo.cleanup:NOT_REACHED") finally: checkpoints.append("foo.cleanup.finally") checkpoints = [] context = make_runtime_context() with pytest.raises(FixtureSetupError): with scoped_context_layer(context): use_fixture(foo, context, checkpoints) # -- NEVER-REACHED: checkpoints.append("scoped-block:NOT_REACHED") # -- ENSURE: fixture-cleanup (foo.cleanup) is called (EARLY-CLEANUP). assert checkpoints == ["foo.setup.begin", "foo.cleanup.finally"]
def test_fixture_with_args(self): """Ensures that positional args are passed to fixture function.""" @fixture def foo(context, *args, **kwargs): fixture_object = FooFixture.setup(*args, **kwargs) context.foo = fixture_object yield fixture_object fixture_object.cleanup() context = make_runtime_context() with scoped_context_layer(context): the_fixture = use_fixture(foo, context, 1, 2, 3) expected_args = (1, 2, 3) assert the_fixture.args == expected_args
def test_fixture_with_args(self): """Ensures that positional args are passed to fixture function.""" @fixture def foo(context, *args, **kwargs): fixture_object = FooFixture.setup(*args, **kwargs) context.foo = fixture_object yield fixture_object fixture_object.cleanup() context = make_runtime_context() with scoped_context_layer(context): the_fixture = use_fixture(foo, context, 1, 2, 3) expected_args = (1, 2, 3) assert the_fixture.args == expected_args
def test_setup_error_with_context_cleanup2_then_cleanup_is_called(self): # -- CASE: Fixture is generator-function (contextmanager) # NOTE: Explicit use of context.add_cleanup() @fixture def foo(context, checkpoints, **kwargs): 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_1") checkpoints.append("foo.setup.done:NOT_REACHED") yield checkpoints.append("foo.cleanup: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 assert checkpoints == ["foo.setup_with_error:foo_1", "cleanup_foo:foo_1"]
def test_basic_lifecycle(self): # -- NOTE: Use explicit checks instead of assert-helper functions. @fixture def foo(context, checkpoints, *args, **kwargs): checkpoints.append("foo.setup") yield FooFixture() checkpoints.append("foo.cleanup") checkpoints = [] context = make_runtime_context() with scoped_context_layer(context): the_fixture = use_fixture(foo, context, checkpoints) # -- ENSURE: Fixture setup is performed (and as expected) assert isinstance(the_fixture, FooFixture) assert checkpoints == ["foo.setup"] checkpoints.append("scoped-block") # -- ENSURE: Fixture cleanup was performed assert checkpoints == ["foo.setup", "scoped-block", "foo.cleanup"]
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)
def test_basic_lifecycle(self): # -- NOTE: Use explicit checks instead of assert-helper functions. @fixture def foo(context, checkpoints, *args, **kwargs): checkpoints.append("foo.setup") yield FooFixture() checkpoints.append("foo.cleanup") checkpoints = [] context = make_runtime_context() with scoped_context_layer(context): the_fixture = use_fixture(foo, context, checkpoints) # -- ENSURE: Fixture setup is performed (and as expected) assert isinstance(the_fixture, FooFixture) assert checkpoints == ["foo.setup"] checkpoints.append("scoped-block") # -- ENSURE: Fixture cleanup was performed assert checkpoints == ["foo.setup", "scoped-block", "foo.cleanup"]
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)
def test_invalid_fixture_function(self): """Test invalid generator function with more than one yield-statement (not a valid fixture/context-manager). """ @fixture def invalid_fixture(context, checkpoints, *args, **kwargs): checkpoints.append("bad.setup") yield FooFixture(*args, **kwargs) checkpoints.append("bad.cleanup") yield None # -- SYNDROME HERE: More than one yield-statement # -- PERFORM-TEST: checkpoints = [] context = make_runtime_context() with pytest.raises(InvalidFixtureError): with scoped_context_layer(context): the_fixture = use_fixture(invalid_fixture, context, checkpoints) assert checkpoints == ["bad.setup"] checkpoints.append("scoped-block") # -- VERIFY: Ensure normal cleanup-parts were performed. assert checkpoints == ["bad.setup", "scoped-block", "bad.cleanup"]
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"]
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"]
def test_invalid_fixture_function(self): """Test invalid generator function with more than one yield-statement (not a valid fixture/context-manager). """ @fixture def invalid_fixture(context, checkpoints, *args, **kwargs): checkpoints.append("bad.setup") yield FooFixture(*args, **kwargs) checkpoints.append("bad.cleanup") yield None # -- SYNDROME HERE: More than one yield-statement # -- PERFORM-TEST: checkpoints = [] context = make_runtime_context() with pytest.raises(InvalidFixtureError): with scoped_context_layer(context): the_fixture = use_fixture(invalid_fixture, context, checkpoints) assert checkpoints == ["bad.setup"] checkpoints.append("scoped-block") # -- VERIFY: Ensure normal cleanup-parts were performed. assert checkpoints == ["bad.setup", "scoped-block", "bad.cleanup"]
def test_bad_with_setup_error(self): # -- SAD: cleanup_fixture() part is called when setup-error occurs, # but not the cleanup-part of the generator (generator-magic). @fixture def bad_with_setup_error(context, checkpoints, *args, **kwargs): checkpoints.append("bad.setup_with_error") raise FixtureSetupError() yield FooFixture(*args, **kwargs) checkpoints.append("bad.cleanup:NOT_REACHED") # -- PERFORM-TEST: the_fixture = None checkpoints = [] context = make_runtime_context() bad_fixture = bad_with_setup_error with pytest.raises(FixtureSetupError): with scoped_context_layer(context): the_fixture = use_fixture(bad_fixture, context, checkpoints) checkpoints.append("scoped-block:NOT_REACHED") # -- VERIFY: Ensure normal cleanup-parts were performed. # * SAD: fixture-cleanup is not called due to fixture-setup error. assert the_fixture is None # -- NEVER STORED: Due to setup error. assert checkpoints == ["bad.setup_with_error"]
def test_bad_with_setup_error(self): # -- SAD: cleanup_fixture() part is called when setup-error occurs, # but not the cleanup-part of the generator (generator-magic). @fixture def bad_with_setup_error(context, checkpoints, *args, **kwargs): checkpoints.append("bad.setup_with_error") raise FixtureSetupError() yield FooFixture(*args, **kwargs) checkpoints.append("bad.cleanup:NOT_REACHED") # -- PERFORM-TEST: the_fixture = None checkpoints = [] context = make_runtime_context() bad_fixture = bad_with_setup_error with pytest.raises(FixtureSetupError): with scoped_context_layer(context): the_fixture = use_fixture(bad_fixture, context, checkpoints) checkpoints.append("scoped-block:NOT_REACHED") # -- VERIFY: Ensure normal cleanup-parts were performed. # * SAD: fixture-cleanup is not called due to fixture-setup error. assert the_fixture is None # -- NEVER STORED: Due to setup error. assert checkpoints == ["bad.setup_with_error"]
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)