Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/8016.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed only one doctest being collected when using ``pytest --doctest-modules path/to/an/__init__.py``.
14 changes: 8 additions & 6 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,12 +765,14 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
self._notfound.append((report_arg, col))
continue

# If __init__.py was the only file requested, then the matched node will be
# the corresponding Package, and the first yielded item will be the __init__
# 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":
assert isinstance(matching[0], nodes.Collector)
# If __init__.py was the only file requested, then the matched
# node will be the corresponding Package (by default), and the
# first yielded item will be the __init__ 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" and isinstance(
matching[0], Package
):
try:
yield next(iter(matching[0].collect()))
except StopIteration:
Expand Down
11 changes: 8 additions & 3 deletions testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ def my_func():
assert isinstance(items[0].parent, DoctestModule)
assert items[0].parent is items[1].parent

def test_collect_module_two_doctest_no_modulelevel(self, pytester: Pytester):
@pytest.mark.parametrize("filename", ["__init__", "whatever"])
def test_collect_module_two_doctest_no_modulelevel(
self, pytester: Pytester, filename: str,
) -> None:
path = pytester.makepyfile(
whatever="""
**{
filename: """
'# Empty'
def my_func():
">>> magic = 42 "
Expand All @@ -84,7 +88,8 @@ def another():
# This is another function
>>> import os # this one does have a doctest
'''
"""
""",
},
)
for p in (path, pytester.path):
items, reprec = pytester.inline_genitems(p, "--doctest-modules")
Expand Down