Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Anthony Shaw
Anthony Sottile
Anton Grinevich
Anton Lodder
Anton Zhilin
Antony Lee
Arel Cordero
Arias Emmanuel
Expand Down
2 changes: 2 additions & 0 deletions changelog/13248.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed an issue where passing a ``scope`` in :py:func:`Metafunc.parametrize <pytest.Metafunc.parametrize>` with ``indirect=True``
could result in other fixtures being unable to depend on the parametrized fixture.
7 changes: 6 additions & 1 deletion src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,12 @@ def _get_active_fixturedef(
param_index = 0
scope = fixturedef._scope
self._check_fixturedef_without_param(fixturedef)
self._check_scope(fixturedef, scope)
# The parametrize invocation scope only controls caching behavior while
# allowing wider-scoped fixtures to keep depending on the parametrized
# fixture. Scope control is enforced for parametrized fixtures
# by recreating the whole fixture tree on parameter change.
# Hence `fixturedef._scope`, not `scope`.
self._check_scope(fixturedef, fixturedef._scope)
subrequest = SubRequest(
self, scope, param, param_index, fixturedef, _ispytest=True
)
Expand Down
28 changes: 28 additions & 0 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -5009,3 +5009,31 @@ def test_result():
)
result = pytester.runpytest()
assert result.ret == 0


def test_parametrized_fixture_scope_allowed(pytester: Pytester) -> None:
"""
Make sure scope from parametrize does not affect fixture's ability to be
depended upon.

Regression test for #13248
"""
pytester.makepyfile(
"""
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 value"], indirect=True, scope="function")
def test_foo(another_fixture):
assert another_fixture == "a value"
"""
)
result = pytester.runpytest()
result.assert_outcomes(passed=1)