Skip to content

Commit c554c3d

Browse files
authored
Merge pull request #11968 from pytest-dev/backport-11957-to-8.0.x
[8.0.x] main: fix reversed collection order in Session
2 parents e6f6be3 + a6851e3 commit c554c3d

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

Diff for: changelog/11937.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a regression in pytest 8.0.0 whereby items would be collected in reverse order in some circumstances.

Diff for: src/_pytest/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
898898

899899
# Prune this level.
900900
any_matched_in_collector = False
901-
for node in subnodes:
901+
for node in reversed(subnodes):
902902
# Path part e.g. `/a/b/` in `/a/b/test_file.py::TestIt::test_it`.
903903
if isinstance(matchparts[0], Path):
904904
is_match = node.path == matchparts[0]

Diff for: testing/acceptance_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def test_issue88_initial_file_multinodes(self, pytester: Pytester) -> None:
240240
pytester.copy_example("issue88_initial_file_multinodes")
241241
p = pytester.makepyfile("def test_hello(): pass")
242242
result = pytester.runpytest(p, "--collect-only")
243-
result.stdout.fnmatch_lines(["*Module*test_issue88*", "*MyFile*test_issue88*"])
243+
result.stdout.fnmatch_lines(["*MyFile*test_issue88*", "*Module*test_issue88*"])
244244

245245
def test_issue93_initialnode_importing_capturing(self, pytester: Pytester) -> None:
246246
pytester.makeconftest(

Diff for: testing/test_collection.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -591,12 +591,12 @@ def pytest_collect_file(file_path, parent):
591591
hookrec.assert_contains(
592592
[
593593
("pytest_collectstart", "collector.path == collector.session.path"),
594-
("pytest_collectstart", "collector.__class__.__name__ == 'Module'"),
595-
("pytest_pycollect_makeitem", "name == 'test_func'"),
596594
(
597595
"pytest_collectstart",
598596
"collector.__class__.__name__ == 'SpecialFile'",
599597
),
598+
("pytest_collectstart", "collector.__class__.__name__ == 'Module'"),
599+
("pytest_pycollect_makeitem", "name == 'test_func'"),
600600
("pytest_collectreport", "report.nodeid.startswith(p.name)"),
601601
]
602602
)
@@ -670,6 +670,23 @@ def test_method(self):
670670
# ensure we are reporting the collection of the single test item (#2464)
671671
assert [x.name for x in self.get_reported_items(hookrec)] == ["test_method"]
672672

673+
def test_collect_parametrized_order(self, pytester: Pytester) -> None:
674+
p = pytester.makepyfile(
675+
"""
676+
import pytest
677+
678+
@pytest.mark.parametrize('i', [0, 1, 2])
679+
def test_param(i): ...
680+
"""
681+
)
682+
items, hookrec = pytester.inline_genitems(f"{p}::test_param")
683+
assert len(items) == 3
684+
assert [item.nodeid for item in items] == [
685+
"test_collect_parametrized_order.py::test_param[0]",
686+
"test_collect_parametrized_order.py::test_param[1]",
687+
"test_collect_parametrized_order.py::test_param[2]",
688+
]
689+
673690

674691
class Test_getinitialnodes:
675692
def test_global_file(self, pytester: Pytester) -> None:

0 commit comments

Comments
 (0)