Skip to content

Commit ada9977

Browse files
Anton3nicoddemus
andauthored
Prevent parametrize with scope from breaking fixture dependencies (#13249)
* Prevent parametrize with scope from breaking fixture dependencies * Add myself to AUTHORS * Fix the regression test * Assert fixture value inside the test --------- Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 611f336 commit ada9977

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

changelog/13248.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed an issue where passing a ``scope`` in :py:func:`Metafunc.parametrize <pytest.Metafunc.parametrize>` with ``indirect=True``
2+
could result in other fixtures being unable to depend on the parametrized fixture.

src/_pytest/fixtures.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,12 @@ def _get_active_fixturedef(
605605
param_index = 0
606606
scope = fixturedef._scope
607607
self._check_fixturedef_without_param(fixturedef)
608-
self._check_scope(fixturedef, scope)
608+
# The parametrize invocation scope only controls caching behavior while
609+
# allowing wider-scoped fixtures to keep depending on the parametrized
610+
# fixture. Scope control is enforced for parametrized fixtures
611+
# by recreating the whole fixture tree on parameter change.
612+
# Hence `fixturedef._scope`, not `scope`.
613+
self._check_scope(fixturedef, fixturedef._scope)
609614
subrequest = SubRequest(
610615
self, scope, param, param_index, fixturedef, _ispytest=True
611616
)

testing/python/fixtures.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5009,3 +5009,31 @@ def test_result():
50095009
)
50105010
result = pytester.runpytest()
50115011
assert result.ret == 0
5012+
5013+
5014+
def test_parametrized_fixture_scope_allowed(pytester: Pytester) -> None:
5015+
"""
5016+
Make sure scope from parametrize does not affect fixture's ability to be
5017+
depended upon.
5018+
5019+
Regression test for #13248
5020+
"""
5021+
pytester.makepyfile(
5022+
"""
5023+
import pytest
5024+
5025+
@pytest.fixture(scope="session")
5026+
def my_fixture(request):
5027+
return getattr(request, "param", None)
5028+
5029+
@pytest.fixture(scope="session")
5030+
def another_fixture(my_fixture):
5031+
return my_fixture
5032+
5033+
@pytest.mark.parametrize("my_fixture", ["a value"], indirect=True, scope="function")
5034+
def test_foo(another_fixture):
5035+
assert another_fixture == "a value"
5036+
"""
5037+
)
5038+
result = pytester.runpytest()
5039+
result.assert_outcomes(passed=1)

0 commit comments

Comments
 (0)