Skip to content

Commit

Permalink
Fix RuntimeError when trying to collect package with "__init__.py" only
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Aug 5, 2019
1 parent 1a66a50 commit 198fcd8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/4344.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix RuntimeError/StopIteration when trying to collect package with "__init__.py" only.
8 changes: 7 additions & 1 deletion src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,13 @@ def _collect(self, arg):
# Module itself, so just use that. If this special case isn't taken, then all
# the files in the package will be yielded.
if argpath.basename == "__init__.py":
yield next(m[0].collect())
try:
yield next(m[0].collect())
except StopIteration:
# The package collects nothing with only an __init__.py
# file in it, which gets ignored by the default
# "python_files" option.
pass
return
yield from m

Expand Down
12 changes: 12 additions & 0 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,18 @@ def test_collect_pkg_init_and_file_in_args(testdir):
)


def test_collect_pkg_init_only(testdir):
subdir = testdir.mkdir("sub")
init = subdir.ensure("__init__.py")
init.write("def test_init(): pass")

result = testdir.runpytest(str(init))
result.stdout.fnmatch_lines(["*no tests ran in*"])

result = testdir.runpytest("-v", "-o", "python_files=*.py", str(init))
result.stdout.fnmatch_lines(["sub/__init__.py::test_init PASSED*", "*1 passed in*"])


@pytest.mark.skipif(
not hasattr(py.path.local, "mksymlinkto"),
reason="symlink not available on this platform",
Expand Down

0 comments on commit 198fcd8

Please sign in to comment.