Skip to content

A fixture with autouse set to False will always be used if it overrides a fixture with autouse set to True #3225

Open

Description

@bngo92
  • Include a detailed description of the bug or suggestion
    I took a look at pytest's source code and it looks like pytest records the names of all fixtures with autouse set to True. It then uses this list to determine which fixtures to use, even if the overriding fixture has autouse set to False.
  • pytest and operating system versions
    platform linux2 -- Python 2.7.13, pytest-3.4.0, py-1.5.2, pluggy-0.6.0
  • Minimal example if possible
    conftest.py
@pytest.fixture(autouse=True)
def foo():
    pass

test_me.py:

@pytest.fixture()
def foo():
    assert False

def test_bar(foo):
    pass

def test_baz():
    pass

Returns the following error:

____________________________________________________________________________ ERROR at setup of test_bar _____________________________________________________________________________

    @pytest.fixture()
    def foo():
>       assert False
E       assert False

____________________________________________________________________________ ERROR at setup of test_baz _____________________________________________________________________________

    @pytest.fixture()
    def foo():
>       assert False
E       assert False

I expect only test_bar to fail. Setting autouse to False for foo in conftest.py correctly causes only test_bar to fail.

Activity

  1. ceridwen commented on Mar 12, 2018

    @ceridwen
    Contributor

    I've encountered what may be another manifestation of this bug, only with parametrization. I'm doing some dynamic fixture creation based on command-line options. An autouse=True session fixture takes this fixture as an argument:

    def pytest_configure(config):
        class DynamicFixturePlugin2(object):
            @pytest.fixture(scope='session', params=['foo, 'bar'] if config.getoption('--foo') else [''])
            def baz(self, request):
                return request.param
        config.pluginmanager.register(DynamicFixturePlugin2(), 'baz-fixture')
    

    I realized that I accidentally created a test that uses that same name as a parameter.

    @pytest.mark.parametrize('baz, ['foo', 'bar'])
    def test(baz, ...):
       ....
    

    This test just so happened to be the first test in the file it was in. If I then invoke pytest with this file as the first item in a list of test files and that test is the first test in that file, then I get the following error:

    ScopeMismatch: You tried to access the 'function' scoped fixture 'baz' with a 'session' scoped request object, involved factories
    conftest.py:141:  def autouse_fixture_that_depends_on_dynamic_fixture(request, baz, ...)
    

    If it's not related, tell me and I'll file a separate bug.

  2. jbbqqf commented on May 9, 2026

    @jbbqqf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions