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

Autouse session-scoped fixtures are sometimes torn down in the wrong order #139

Closed
jordanlibrande opened this issue Jan 12, 2024 · 1 comment

Comments

@jordanlibrande
Copy link

jordanlibrande commented Jan 12, 2024

In the pytest documentation https://docs.pytest.org/en/6.2.x/fixture.html#fixture-instantiation-order, fixtures with the autouse flag are ordered specially vs non-autouse fixtures (and the ordering for instantiating fixtures should be reversed when tearing down fixtures https://docs.pytest.org/en/6.2.x/fixture.html#yield-fixtures-recommended).

Teardown ordering is incorrect sometimes (both different from vanilla pytest, and not following the "reverse ordering for teardown" rule) for a session where you have async tests and both explicitly depended session-scoped fixtures and autouse session-scoped fixtures.

Following the autouse ordering, we should also probably ensure not to run autouse fixtures concurrently with explicitly-selected fixtures.

Example code (with pytest-trio and trio mode enabled):

import pytest


@pytest.fixture(scope="session")
def f1():
    print("f1 initialization")
    yield "f1"
    print("f1 teardown")


@pytest.fixture(scope="session")
def f2():
    print("f2 initialization")
    yield "f2"
    print("f2 teardown")


@pytest.fixture(scope="session", autouse=True)
def f3():
    print("f3 initialization")
    yield "f3"
    print("f3 teardown")


async def test_foo(f1: str):
    print("Running test_foo")


async def test_bar(f2: str):
    print("Running test_bar")

Output:

tests/repro.py::test_bar
f3 initialization   <<<<<<<<<<<< initialization ordering is fine
f2 initialization

Running test_bar
PASSED
tests/repro.py::test_foo f1 initialization

Running test_foo
PASSEDf1 teardown
f3 teardown        <<<<<<<<<<< Unexpected, f3 teardown should happen last
f2 teardown
@jordanlibrande
Copy link
Author

jordanlibrande commented Jan 12, 2024

This is a bug with pytest itself, see issue pytest-dev/pytest#1489

@jordanlibrande jordanlibrande closed this as not planned Won't fix, can't repro, duplicate, stale Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant