Skip to content

function-scoped fixture run before session-scoped fixture #5303

Open
@brianmaissy

Description

@brianmaissy

This is a possible regression of #2405, or possibly a previously unconsidered edge case.

I have a case where a conftest in a subdirectory overrides a fixture with the same name from the enclosing directory's conftest, and this fact causes the fixtures to be set up in the wrong order (a function-scoped fixture is run before a session-scoped fixture).

Here's the example:

conftest.py
subdirectory/
    conftest.py
    test_it.py

The root conftest:

@pytest.fixture(scope='session')
def fixture_one():
    print('setting up fixture one (session-scoped)')


@pytest.fixture(autouse=True)
def fixture_two():
    print('setting up fixture two (function-scoped autouse)')
    yield
    print('cleaning up fixture two (function-scoped autouse)')


@pytest.fixture()
def fixture_three(fixture_one):
    print('setting up fixture three (function-scoped)')

The subdirectory conftest:

# overrides the original fixture_three, and is also dependent on it
@pytest.fixture()
def fixture_three(fixture_three):
    print('setting up overridden fixture three (function-scoped)')

test_it.py:

def test_it(fixture_three):
    pass

The output:

$ py.test -s
============================= test session starts ==============================
platform linux -- Python 3.6.7, pytest-4.5.0, py-1.7.0, pluggy-0.11.0
rootdir: /mnt/hgfs/test
collected 1 item                                                               

subdirectory/test_it.py
setting up fixture two (function-scoped autouse)
setting up fixture one (session-scoped)
setting up fixture three (function-scoped)
setting up overridden fixture three (function-scoped)
.cleaning up fixture two (function-scoped autouse)

As you can see, fixture_two (the autouse function-scoped fixture) is run before fixture_one, which is the session-scoped dependency of the original fixture_three (and an indirect dependency of the overridden fixture_three).

Renaming the second fixture_three solves the problem.

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