Skip to content

Commit fdf3aa3

Browse files
authored
Merge pull request #12329 from pytest-dev/fix-package-scope-reorder
fixtures: fix non-working package-scope parametrization reordering
2 parents 8d00811 + 1acf56d commit fdf3aa3

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

changelog/12328.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a regression in pytest 8.0.0 where package-scoped parameterized items were not correctly reordered to minimize setups/teardowns in some cases.

src/_pytest/fixtures.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ def get_parametrized_fixture_keys(
187187
if scope is Scope.Session:
188188
scoped_item_path = None
189189
elif scope is Scope.Package:
190-
scoped_item_path = item.path
190+
# Package key = module's directory.
191+
scoped_item_path = item.path.parent
191192
elif scope is Scope.Module:
192193
scoped_item_path = item.path
193194
elif scope is Scope.Class:

testing/python/fixtures.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4274,6 +4274,39 @@ def test_func(self, f2, f1, m2):
42744274
request = TopRequest(items[0], _ispytest=True)
42754275
assert request.fixturenames == "s1 p1 m1 m2 c1 f2 f1".split()
42764276

4277+
def test_parametrized_package_scope_reordering(self, pytester: Pytester) -> None:
4278+
"""A paramaterized package-scoped fixture correctly reorders items to
4279+
minimize setups & teardowns.
4280+
4281+
Regression test for #12328.
4282+
"""
4283+
pytester.makepyfile(
4284+
__init__="",
4285+
conftest="""
4286+
import pytest
4287+
@pytest.fixture(scope="package", params=["a", "b"])
4288+
def fix(request):
4289+
return request.param
4290+
""",
4291+
test_1="def test1(fix): pass",
4292+
test_2="def test2(fix): pass",
4293+
)
4294+
4295+
result = pytester.runpytest("--setup-plan")
4296+
assert result.ret == ExitCode.OK
4297+
result.stdout.fnmatch_lines(
4298+
[
4299+
" SETUP P fix['a']",
4300+
" test_1.py::test1[a] (fixtures used: fix, request)",
4301+
" test_2.py::test2[a] (fixtures used: fix, request)",
4302+
" TEARDOWN P fix['a']",
4303+
" SETUP P fix['b']",
4304+
" test_1.py::test1[b] (fixtures used: fix, request)",
4305+
" test_2.py::test2[b] (fixtures used: fix, request)",
4306+
" TEARDOWN P fix['b']",
4307+
],
4308+
)
4309+
42774310
def test_multiple_packages(self, pytester: Pytester) -> None:
42784311
"""Complex test involving multiple package fixtures. Make sure teardowns
42794312
are executed in order.

0 commit comments

Comments
 (0)