Skip to content

fixtures: fix non-working package-scope parametrization reordering #12329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2024
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 changelog/12328.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +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.
3 changes: 2 additions & 1 deletion src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ def get_parametrized_fixture_keys(
if scope is Scope.Session:
scoped_item_path = None
elif scope is Scope.Package:
scoped_item_path = item.path
# Package key = module's directory.
scoped_item_path = item.path.parent
elif scope is Scope.Module:
scoped_item_path = item.path
elif scope is Scope.Class:
Expand Down
33 changes: 33 additions & 0 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4274,6 +4274,39 @@ def test_func(self, f2, f1, m2):
request = TopRequest(items[0], _ispytest=True)
assert request.fixturenames == "s1 p1 m1 m2 c1 f2 f1".split()

def test_parametrized_package_scope_reordering(self, pytester: Pytester) -> None:
"""A paramaterized package-scoped fixture correctly reorders items to
minimize setups & teardowns.

Regression test for #12328.
"""
pytester.makepyfile(
__init__="",
conftest="""
import pytest
@pytest.fixture(scope="package", params=["a", "b"])
def fix(request):
return request.param
""",
test_1="def test1(fix): pass",
test_2="def test2(fix): pass",
)

result = pytester.runpytest("--setup-plan")
assert result.ret == ExitCode.OK
result.stdout.fnmatch_lines(
[
" SETUP P fix['a']",
" test_1.py::test1[a] (fixtures used: fix, request)",
" test_2.py::test2[a] (fixtures used: fix, request)",
" TEARDOWN P fix['a']",
" SETUP P fix['b']",
" test_1.py::test1[b] (fixtures used: fix, request)",
" test_2.py::test2[b] (fixtures used: fix, request)",
" TEARDOWN P fix['b']",
],
)

def test_multiple_packages(self, pytester: Pytester) -> None:
"""Complex test involving multiple package fixtures. Make sure teardowns
are executed in order.
Expand Down