How to use the behave.fixture.use_fixture function in behave

To help you get started, we’ve selected a few behave examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github behave / behave / tests / unit / test_fixture.py View on Github external
"""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"]
github behave / behave / tests / unit / test_fixture.py View on Github external
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"]
github behave / behave / tests / unit / test_fixture.py View on Github external
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"]
github behave / behave / tests / unit / test_fixture.py View on Github external
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"]
github behave / behave / tests / unit / test_fixture.py View on Github external
#    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"]
github behave / behave / tests / unit / test_fixture.py View on Github external
# -- 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"]
github behave / behave / tests / unit / test_fixture.py View on Github external
"""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"]
        # -- NOTE: Cleanups occur in reverse order.
github behave / behave / tests / unit / test_fixture.py View on Github external
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"]
github behave / behave / tests / unit / test_fixture.py View on Github external
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
github behave / behave / tests / unit / test_fixture.py View on Github external
        @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"]