Skip to content

Commit 66311ff

Browse files
authored
Merge pull request #8022 from bluetech/doctest-init
main: fix only one doctest collected on pytest --doctest-modules __init__.py
2 parents ea3c0aa + 265cc2c commit 66311ff

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
@@ -775,12 +775,14 @@ def collect(self) -> Iterator[Union[nodes.Item, nodes.Collector]]:
775775
self._notfound.append((report_arg, col))
776776
continue
777777

778-
# If __init__.py was the only file requested, then the matched node will be
779-
# the corresponding Package, and the first yielded item will be the __init__
780-
# Module itself, so just use that. If this special case isn't taken, then all
781-
# the files in the package will be yielded.
782-
if argpath.basename == "__init__.py":
783-
assert isinstance(matching[0], nodes.Collector)
778+
# If __init__.py was the only file requested, then the matched
779+
# node will be the corresponding Package (by default), and the
780+
# first yielded item will be the __init__ Module itself, so
781+
# just use that. If this special case isn't taken, then all the
782+
# files in the package will be yielded.
783+
if argpath.basename == "__init__.py" and isinstance(
784+
matching[0], Package
785+
):
784786
try:
785787
yield next(iter(matching[0].collect()))
786788
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)