Skip to content

Commit ea57c40

Browse files
committed
main: fix reversed collection order in Session
Since we're working with a stack (last in first out), we need to append to it in reverse to preserve the order when popped. Fix #11937.
1 parent 9454fc3 commit ea57c40

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

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.

src/_pytest/main.py

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

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

testing/acceptance_test.py

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

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

testing/test_collection.py

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

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

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

0 commit comments

Comments
 (0)