Skip to content

Commit 07a560f

Browse files
committed
Fix collection error when tests is specified with --doctest-modules
The problem was that _matchnodes would receive two items: [DoctestModule, Module]. It would then collect the first one, *cache it*, and fail to match against the name in the command line. Next, it would reuse the cached item (DoctestModule) instead of collecting the Module which would eventually find the "test" name on it. Added the type of the node to the cache key to avoid this problem, although I'm not a big fan of caches that have different key types. Fix #3843
1 parent f1079a8 commit 07a560f

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

changelog/3843.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix collection error when specifying test functions directly in the command line using ``test.py::test`` syntax together with ``--doctest-module``.

src/_pytest/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,11 +625,12 @@ def _matchnodes(self, matching, names):
625625
resultnodes.append(node)
626626
continue
627627
assert isinstance(node, nodes.Collector)
628-
if node.nodeid in self._node_cache:
629-
rep = self._node_cache[node.nodeid]
628+
key = (type(node), node.nodeid)
629+
if key in self._node_cache:
630+
rep = self._node_cache[key]
630631
else:
631632
rep = collect_one_node(node)
632-
self._node_cache[node.nodeid] = rep
633+
self._node_cache[key] = rep
633634
if rep.passed:
634635
has_matched = False
635636
for x in rep.result:

testing/acceptance_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,16 @@ def join_pythonpath(*dirs):
660660
["*test_world.py::test_other*PASSED*", "*1 passed*"]
661661
)
662662

663+
def test_invoke_test_and_doctestmodules(self, testdir):
664+
p = testdir.makepyfile(
665+
"""
666+
def test():
667+
pass
668+
"""
669+
)
670+
result = testdir.runpytest(str(p) + "::test", "--doctest-modules")
671+
result.stdout.fnmatch_lines(["*1 passed*"])
672+
663673
@pytest.mark.skipif(not hasattr(os, "symlink"), reason="requires symlinks")
664674
def test_cmdline_python_package_symlink(self, testdir, monkeypatch):
665675
"""

0 commit comments

Comments
 (0)