-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Closed
Labels
topic: fixturesanything involving fixtures directly or indirectlyanything involving fixtures directly or indirectly
Description
Reproducer:
import pytest
@pytest.fixture(scope="session")
def my_fixture(request):
return getattr(request, "param", None)
@pytest.fixture(scope="session")
def another_fixture(my_fixture):
return my_fixture
@pytest.mark.parametrize("my_fixture", ["a"], indirect=True, scope="function")
def test_foo(another_fixture):
pass
Produces the following error:
ScopeMismatch: You tried to access the function scoped fixture my_fixture with a session scoped request object. Requesting fixture stack:
def another_fixture(my_fixture):
Requested fixture:
my_fixture
parametrize
with scope
is sometimes important for test isolation:
#9287 (comment)
The bug is not reproduced consistently, it may be hidden if the fixture is already evaluated by a function
-scoped fixture. For example:
import pytest
@pytest.fixture(scope="session")
def my_fixture(request):
return getattr(request, "param", None)
@pytest.fixture(scope="session")
def another_fixture(my_fixture):
return my_fixture
# autouse to make this fixture run first.
@pytest.fixture(scope="function", autouse=True)
def hack(my_fixture):
pass
@pytest.mark.parametrize("my_fixture", ["a"], indirect=True, scope="function")
def test_foo(hack, another_fixture):
pass
This does not return any errors, works as expected.
Metadata
Metadata
Assignees
Labels
topic: fixturesanything involving fixtures directly or indirectlyanything involving fixtures directly or indirectly