Skip to content
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

py36+ tests are definition ordered #7848

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/5196.feature.rst
@@ -0,0 +1 @@
Tests are now ordered by definition order in more cases.
5 changes: 0 additions & 5 deletions src/_pytest/python.py
Expand Up @@ -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"]:
Expand Down
24 changes: 24 additions & 0 deletions testing/python/collect.py
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions testing/test_collection.py
Expand Up @@ -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")
Expand Down