Skip to content

Commit

Permalink
Fix incorrect collected items report when specifying tests on the com…
Browse files Browse the repository at this point in the history
…mand-line

Fix pytest-dev#2464
  • Loading branch information
nicoddemus committed Jun 3, 2017
1 parent 08b5836 commit 9ac7c28
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 0 additions & 1 deletion _pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,6 @@ def _matchnodes(self, matching, names):
if not has_matched and len(rep.result) == 1 and x.name == "()":
nextnames.insert(0, name)
resultnodes.extend(self.matchnodes([x], nextnames))
node.ihook.pytest_collectreport(report=rep)
return resultnodes

def genitems(self, node):
Expand Down
1 change: 1 addition & 0 deletions changelog/2467.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix incorrect "collected items" report when specifying tests on the command-line.
16 changes: 13 additions & 3 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ def test_collect_topdir(self, testdir):
assert len(colitems) == 1
assert colitems[0].fspath == p

def get_reported_items(self, hookrec):
"""Return pytest.Item instances reported by the pytest_collectreport hook"""
calls = hookrec.getcalls('pytest_collectreport')
return [x for call in calls for x in call.report.result
if isinstance(x, pytest.Item)]

def test_collect_protocol_single_function(self, testdir):
p = testdir.makepyfile("def test_func(): pass")
Expand All @@ -386,9 +391,10 @@ def test_collect_protocol_single_function(self, testdir):
("pytest_collectstart", "collector.fspath == p"),
("pytest_make_collect_report", "collector.fspath == p"),
("pytest_pycollect_makeitem", "name == 'test_func'"),
("pytest_collectreport", "report.nodeid.startswith(p.basename)"),
("pytest_collectreport", "report.nodeid == ''")
("pytest_collectreport", "report.result[0].name == 'test_func'"),
])
# ensure we are reporting the collection of the single test item (#2464)
assert len(self.get_reported_items(hookrec)) == 1

def test_collect_protocol_method(self, testdir):
p = testdir.makepyfile("""
Expand All @@ -407,6 +413,8 @@ def test_method(self):
assert items[0].name == "test_method"
newid = items[0].nodeid
assert newid == normid
# ensure we are reporting the collection of the single test item (#2464)
assert len(self.get_reported_items(hookrec)) == 1

def test_collect_custom_nodes_multi_id(self, testdir):
p = testdir.makepyfile("def test_func(): pass")
Expand Down Expand Up @@ -439,6 +447,7 @@ def pytest_collect_file(path, parent):
#("pytest_collectreport",
# "report.fspath == %r" % str(rcol.fspath)),
])
assert len(self.get_reported_items(hookrec)) == 2

def test_collect_subdir_event_ordering(self, testdir):
p = testdir.makepyfile("def test_func(): pass")
Expand Down Expand Up @@ -495,11 +504,12 @@ class TestClass(object):
def test_method(self):
pass
""")
arg = p.basename + ("::TestClass::test_method")
arg = p.basename + "::TestClass::test_method"
items, hookrec = testdir.inline_genitems(arg)
assert len(items) == 1
item, = items
assert item.nodeid.endswith("TestClass::()::test_method")
assert len(self.get_reported_items(hookrec)) == 1

class Test_getinitialnodes(object):
def test_global_file(self, testdir, tmpdir):
Expand Down

0 comments on commit 9ac7c28

Please sign in to comment.