Skip to content

Commit b71f873

Browse files
committed
[4.6] Fix RuntimeError when trying to collect package with "__init__.py" only
Fixes #4344.
1 parent a19ae2a commit b71f873

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
@@ -621,7 +621,13 @@ def _collect(self, arg):
621621
# Module itself, so just use that. If this special case isn't taken, then all
622622
# the files in the package will be yielded.
623623
if argpath.basename == "__init__.py":
624-
yield next(m[0].collect())
624+
try:
625+
yield next(m[0].collect())
626+
except StopIteration:
627+
# The package collects nothing with only an __init__.py
628+
# file in it, which gets ignored by the default
629+
# "python_files" option.
630+
pass
625631
return
626632
for y in m:
627633
yield y

testing/test_collection.py

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

12131213

1214+
def test_collect_pkg_init_only(testdir):
1215+
subdir = testdir.mkdir("sub")
1216+
init = subdir.ensure("__init__.py")
1217+
init.write("def test_init(): pass")
1218+
1219+
result = testdir.runpytest(str(init))
1220+
result.stdout.fnmatch_lines(["*no tests ran in*"])
1221+
1222+
result = testdir.runpytest("-v", "-o", "python_files=*.py", str(init))
1223+
result.stdout.fnmatch_lines(["sub/__init__.py::test_init PASSED*", "*1 passed in*"])
1224+
1225+
12141226
@pytest.mark.skipif(
12151227
not hasattr(py.path.local, "mksymlinkto"),
12161228
reason="symlink not available on this platform",

0 commit comments

Comments
 (0)