Skip to content

Extending a fixture does not keep its params #1953

Closed
@jmoldow

Description

@jmoldow
  • Include a detailed description of the bug or suggestion

When overriding a fixture, pytest allows use of the "super" fixture (http://docs.pytest.org/en/latest/fixture.html#override-a-fixture-on-a-folder-conftest-level). However, using such a fixture seems to not work the same as using other fixtures.

If I have a parametrized fixture foo, and another fixture bar that depends on foo, then bar will be parametrized on foo. And if there is a test_bar that uses bar, then test_bar will be run once for each of the params of foo.

Suppose there are two bar fixtures, and one extends the other. At test collection time, it seems that test_bar does not know it is supposed to be parametrized off the params of foo. But at test running time, pytest figures out that there is supposed to be parameterization, and fails itself.

I'm not sure if this is a missing feature, or a bug.

If this is intentional, is there any way to have the bar override automatically bring in the dependencies of the super fixture? Right now, the only workaround I can figure out is to do:

@pytest.fixture
def bar(bar, foo):
    ...

even if the new bar doesn't directly use foo.

Also, I don't know of any workaround if you extend a fixture that has params, e.g. if you want to extend foo based off the super fixture.

  • pip list of the virtual environment you are using

$ .tox/py35/bin/pip list
hypothesis (3.4.2)
mock (2.0.0)
nose (1.3.7)
pbr (1.10.0)
pip (8.1.2)
py (1.4.31)
pytest (3.0.3.dev0, /Users/jmoldow/repos/pytest)
requests (2.11.1)
setuptools (27.2.0)
six (1.10.0)
wheel (0.30.0a0)

  • pytest and operating system versions

pytest 3.0.2 and pytest 3.0.3.dev0 (7660a19).

OS X Yosemite Version 10.10.5

  • Minimal example if possible

In testing/python/fixture.py, add the tests

    def test_extend_parametrized_fixture(self, testdir):
        testdir.makeconftest("""
            import pytest

            @pytest.fixture(params=[1])
            def foo(request):
                return request.param
        """)
        testfile = testdir.makepyfile("""
            import pytest

            @pytest.fixture
            def foo(foo):
                return foo * 2

            def test_spam(foo):
                assert foo == 2
        """)
        result = testdir.runpytest(testfile)
        result.stdout.fnmatch_lines(["*1 passed*"])

    def test_extend_fixture_with_parametrized_dependency(self, testdir):
        testdir.makeconftest("""
            import pytest

            @pytest.fixture(params=[1])
            def foo(request):
                return request.param

            @pytest.fixture
            def bar(foo):
                return foo
        """)
        testfile = testdir.makepyfile("""
            import pytest

            @pytest.fixture
            def bar(bar):
                return bar * 2

            def test_spam(bar):
                assert bar == 2
        """)
        result = testdir.runpytest(testfile)
        result.stdout.fnmatch_lines(["*1 passed*"])

The output from those tests is attached.
captured_output.txt

If you manually re-specify the foo dependency, then this test passes:

    def test_extend_fixture_with_parametrized_dependency(self, testdir):
        testdir.makeconftest("""
            import pytest

            @pytest.fixture(params=[1])
            def foo(request):
                return request.param

            @pytest.fixture
            def bar(foo):
                return foo
        """)
        testfile = testdir.makepyfile("""
            import pytest

            @pytest.fixture
            def bar(bar, foo):
                return bar * 2

            def test_spam(bar):
                assert bar == 2
        """)
        result = testdir.runpytest(testfile)
        result.stdout.fnmatch_lines(["*1 passed*"])

But I don't know of any way to fix the test_extend_parametrized_fixture test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions