Skip to content

Commit dc8af18

Browse files
authored
Merge pull request #4745 from blueyed/test_collect_pkg_init_and_file_in_args
Fix handling of pkg init and test file via args
2 parents 386e801 + 61b9246 commit dc8af18

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

changelog/4745.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix/improve collection of args when passing in ``__init__.py`` and a test file.

src/_pytest/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ def _collect(self, arg):
582582
col = self._node_cache[argpath]
583583
else:
584584
collect_root = self._pkg_roots.get(argpath.dirname, self)
585-
col = collect_root._collectfile(argpath)
585+
col = collect_root._collectfile(argpath, handle_dupes=False)
586586
if col:
587587
self._node_cache[argpath] = col
588588
m = self.matchnodes(col, names)

testing/test_collection.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,3 +1157,32 @@ def test_collectignore_via_conftest(testdir, monkeypatch):
11571157

11581158
result = testdir.runpytest()
11591159
assert result.ret == EXIT_NOTESTSCOLLECTED
1160+
1161+
1162+
def test_collect_pkg_init_and_file_in_args(testdir):
1163+
subdir = testdir.mkdir("sub")
1164+
init = subdir.ensure("__init__.py")
1165+
init.write("def test_init(): pass")
1166+
p = subdir.ensure("test_file.py")
1167+
p.write("def test_file(): pass")
1168+
1169+
# NOTE: without "-o python_files=*.py" this collects test_file.py twice.
1170+
# This changed/broke with "Add package scoped fixtures #2283" (2b1410895)
1171+
# initially (causing a RecursionError).
1172+
result = testdir.runpytest("-v", str(init), str(p))
1173+
result.stdout.fnmatch_lines(
1174+
[
1175+
"sub/test_file.py::test_file PASSED*",
1176+
"sub/test_file.py::test_file PASSED*",
1177+
"*2 passed in*",
1178+
]
1179+
)
1180+
1181+
result = testdir.runpytest("-v", "-o", "python_files=*.py", str(init), str(p))
1182+
result.stdout.fnmatch_lines(
1183+
[
1184+
"sub/__init__.py::test_init PASSED*",
1185+
"sub/test_file.py::test_file PASSED*",
1186+
"*2 passed in*",
1187+
]
1188+
)

0 commit comments

Comments
 (0)