#7737·pytest

Parametrized fixture being used by other fixtures

Author: nicoddemusCreated Sep 10, 2020Updated Sep 22, 2026
Labelstopic: fixtures

This issue is split from https://github.com/pytest-dev/pytest/issues/1953.

The following test shows a case which is not currently supported: foo is parametrized, and used indirectly through bar, which is also overwritten closer to the test:

    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 current fixture parametrization mechanism implemented in FixtureManager doesn't follow the fixture dependencies to apply fixture parametrization, it only looks at the argument names of the function explicitly (those in Metafunc).

More investigation is needed.

Workaround

A workaround is to re-specify the dependency to foo in bar.

    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*"])

Thanks to @jmoldow for the original report.