Skip to content

Commit 265cc2c

Browse files
committed
main: fix only one doctest collected on pytest --doctest-modules __init__.py
When --doctest-modules is used, an `__init__.py` file is not a `Package` but a `DoctestModule`, but some collection code assumed that `__init__.py` implies a `Package`. That code caused only a single test to be collected in the scenario in the subject. Tighten up this check to explicitly check for `Package`. There are better solutions, but for another time. Report & test by Nick Gates <[email protected]>.
1 parent e986d84 commit 265cc2c

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

changelog/8016.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed only one doctest being collected when using ``pytest --doctest-modules path/to/an/__init__.py``.

src/_pytest/main.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -765,12 +765,14 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
765765
self._notfound.append((report_arg, col))
766766
continue
767767

768-
# If __init__.py was the only file requested, then the matched node will be
769-
# the corresponding Package, and the first yielded item will be the __init__
770-
# Module itself, so just use that. If this special case isn't taken, then all
771-
# the files in the package will be yielded.
772-
if argpath.basename == "__init__.py":
773-
assert isinstance(matching[0], nodes.Collector)
768+
# If __init__.py was the only file requested, then the matched
769+
# node will be the corresponding Package (by default), and the
770+
# first yielded item will be the __init__ Module itself, so
771+
# just use that. If this special case isn't taken, then all the
772+
# files in the package will be yielded.
773+
if argpath.basename == "__init__.py" and isinstance(
774+
matching[0], Package
775+
):
774776
try:
775777
yield next(iter(matching[0].collect()))
776778
except StopIteration:

testing/test_doctest.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,13 @@ def my_func():
6868
assert isinstance(items[0].parent, DoctestModule)
6969
assert items[0].parent is items[1].parent
7070

71-
def test_collect_module_two_doctest_no_modulelevel(self, pytester: Pytester):
71+
@pytest.mark.parametrize("filename", ["__init__", "whatever"])
72+
def test_collect_module_two_doctest_no_modulelevel(
73+
self, pytester: Pytester, filename: str,
74+
) -> None:
7275
path = pytester.makepyfile(
73-
whatever="""
76+
**{
77+
filename: """
7478
'# Empty'
7579
def my_func():
7680
">>> magic = 42 "
@@ -84,7 +88,8 @@ def another():
8488
# This is another function
8589
>>> import os # this one does have a doctest
8690
'''
87-
"""
91+
""",
92+
},
8893
)
8994
for p in (path, pytester.path):
9095
items, reprec = pytester.inline_genitems(p, "--doctest-modules")

0 commit comments

Comments
 (0)