From 94b3e9d82828c282bcbb4d7580b4282960cbbaa5 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 3 Oct 2020 08:51:13 -0700 Subject: [PATCH] py36+ tests are definition ordered --- changelog/5196.feature.rst | 1 + src/_pytest/python.py | 5 ----- testing/python/collect.py | 24 ++++++++++++++++++++++++ testing/test_collection.py | 8 ++++---- 4 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 changelog/5196.feature.rst diff --git a/changelog/5196.feature.rst b/changelog/5196.feature.rst new file mode 100644 index 00000000000..40db78be84e --- /dev/null +++ b/changelog/5196.feature.rst @@ -0,0 +1 @@ +Tests are now ordered by definition order in more cases. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index e0a295a065b..571e9572c1d 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -442,11 +442,6 @@ def collect(self) -> Iterable[Union[nodes.Item, nodes.Collector]]: else: values.append(res) - def sort_key(item): - fspath, lineno, _ = item.reportinfo() - return (str(fspath), lineno) - - values.sort(key=sort_key) return values def _genfunctions(self, name: str, funcobj) -> Iterator["Function"]: diff --git a/testing/python/collect.py b/testing/python/collect.py index 01294039860..2407140be24 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -752,6 +752,30 @@ def test_a(y): assert len(colitems) == 2 assert [item.name for item in colitems] == ["test_b", "test_a"] + def test_ordered_by_definition_order(self, testdir): + testdir.makepyfile( + """\ + class Test1: + def test_foo(): pass + def test_bar(): pass + class Test2: + def test_foo(): pass + test_bar = Test1.test_bar + """ + ) + result = testdir.runpytest("--collect-only") + result.stdout.fnmatch_lines( + [ + "*Class Test1*", + "*Function test_foo*", + "*Function test_bar*", + "*Class Test2*", + # previously the order was flipped due to Test1.test_bar reference + "*Function test_foo*", + "*Function test_bar*", + ] + ) + class TestConftestCustomization: def test_pytest_pycollect_module(self, testdir): diff --git a/testing/test_collection.py b/testing/test_collection.py index 3cb342a93d5..500d142082e 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -677,15 +677,15 @@ def testmethod_two(self, arg0): assert len(items) == 4 assert items[0].name == "testone" assert items[1].name == "testmethod_one" - assert items[2].name == "testmethod_one" - assert items[3].name == "testmethod_two[.[]" + assert items[2].name == "testmethod_two[.[]" + assert items[3].name == "testmethod_one" # let's also test getmodpath here assert items[0].getmodpath() == "testone" assert items[1].getmodpath() == "TestX.testmethod_one" - assert items[2].getmodpath() == "TestY.testmethod_one" # PR #6202: Fix incorrect result of getmodpath method. (Resolves issue #6189) - assert items[3].getmodpath() == "TestY.testmethod_two[.[]" + assert items[2].getmodpath() == "TestY.testmethod_two[.[]" + assert items[3].getmodpath() == "TestY.testmethod_one" s = items[0].getmodpath(stopatmodule=False) assert s.endswith("test_example_items1.testone")