Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks
bngo92 opened this issue Feb 15, 2018 · 1 comment
Labels
topic: fixtures anything involving fixtures directly or indirectly type: bug problem that needs to be addressed

Comments

@bngo92
Copy link

bngo92 commented Feb 15, 2018

  • 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.

@RonnyPfannschmidt RonnyPfannschmidt added type: bug problem that needs to be addressed topic: fixtures anything involving fixtures directly or indirectly labels Feb 15, 2018
@ceridwen
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: fixtures anything involving fixtures directly or indirectly type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

3 participants