Skip to content

Commit bf3aa72

Browse files
authored
Merge pull request #5697 from blueyed/fix-collect-pkg-init
Fix RuntimeError when trying to collect package with "__init__.py" only
2 parents 1a66a50 + 198fcd8 commit bf3aa72

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

changelog/4344.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix RuntimeError/StopIteration when trying to collect package with "__init__.py" only.

src/_pytest/main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,13 @@ def _collect(self, arg):
584584
# Module itself, so just use that. If this special case isn't taken, then all
585585
# the files in the package will be yielded.
586586
if argpath.basename == "__init__.py":
587-
yield next(m[0].collect())
587+
try:
588+
yield next(m[0].collect())
589+
except StopIteration:
590+
# The package collects nothing with only an __init__.py
591+
# file in it, which gets ignored by the default
592+
# "python_files" option.
593+
pass
588594
return
589595
yield from m
590596

testing/test_collection.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,18 @@ def test_collect_pkg_init_and_file_in_args(testdir):
12031203
)
12041204

12051205

1206+
def test_collect_pkg_init_only(testdir):
1207+
subdir = testdir.mkdir("sub")
1208+
init = subdir.ensure("__init__.py")
1209+
init.write("def test_init(): pass")
1210+
1211+
result = testdir.runpytest(str(init))
1212+
result.stdout.fnmatch_lines(["*no tests ran in*"])
1213+
1214+
result = testdir.runpytest("-v", "-o", "python_files=*.py", str(init))
1215+
result.stdout.fnmatch_lines(["sub/__init__.py::test_init PASSED*", "*1 passed in*"])
1216+
1217+
12061218
@pytest.mark.skipif(
12071219
not hasattr(py.path.local, "mksymlinkto"),
12081220
reason="symlink not available on this platform",

0 commit comments

Comments
 (0)