Skip to content

Commit

Permalink
Prevent parametrize with scope from breaking fixture dependencies (#1…
Browse files Browse the repository at this point in the history
…3249)

* 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 <bruno@pytest.org>
  • Loading branch information
Anton3 and nicoddemus authored Mar 9, 2025
1 parent 611f336 commit ada9977
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
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)

0 comments on commit ada9977

Please sign in to comment.