Pytest: Can't override an autouse fixture ordering

Created on 17 Apr 2018  路  8Comments  路  Source: pytest-dev/pytest

pytest 3.5.0 on Python 3.6.5 (macOS). This result surprised me:

import pytest

d = {'k': 'v_init'}

@pytest.fixture(autouse=True)
def fixauto(monkeypatch):
    monkeypatch.setitem(d, 'k', 'v_auto')

@pytest.fixture
def fix(monkeypatch):
    monkeypatch.setitem(d, 'k', 'v_fix')

def test_auto():
    assert d['k'] == 'v_auto'

def test_fix(fix):
    assert d['k'] == 'v_fix'

def test_fixauto_fix(fixauto, fix):
    assert d['k'] == 'v_fix'

def test_fix_fixauto(fix, fixauto):
    assert d['k'] == 'v_auto'

To be clear, first 3 tests are passing OK but the test_fix_fixauto fails. I would have expected that explicitly injecting an autouse fixture could be used to override the usual order. Feature or bug?

easy fixtures docs

Most helpful comment

we should review if the docs on this are clear enough, but otherwise i believe there is nothing we can do

All 8 comments

GitMate.io thinks possibly related issues are https://github.com/pytest-dev/pytest/issues/3225 (A fixture with autouse set to False will always be used if it overrides a fixture with autouse set to True), https://github.com/pytest-dev/pytest/issues/1601 (Overriding autouse fixture with a parametrized fixture does not work), https://github.com/pytest-dev/pytest/issues/1216 (Provide a way to nicely control fixture execution order ), https://github.com/pytest-dev/pytest/issues/668 (autouse fixtures break scope rules), and https://github.com/pytest-dev/pytest/issues/336 (pytest-2.4.0.dev7 crashes with some autouse fixtures).

autouse fixture are always considered before fixtures that are named, so it doesn't matter in which way you order them on a function, the autouse flag takes precedence over the order in the function call

@wimglenn can we close this or do we still need address something?

we should review if the docs on this are clear enough, but otherwise i believe there is nothing we can do

@wojtekerbetowski, @The-Compiler do you plan on fixing that last commit or should I try to change the docs myself?

@NNRepos @The-Compiler - I'm not sure whether I understand the prolem. Is this the coverage on an added test example? What is the rule here? Should examples be excluded from codecov, or executed during that part of the build?

@NNRepos I don't follow - what's there to fix?

@bluetech ok to close this ? My PR was deemed unnecessary so I think we have no doc or code changes to do here 馃憤

Was this page helpful?
0 / 5 - 0 ratings