Skip to content

Commit c34eaaa

Browse files
committed
Pass importmode to import_path in DoctestModule
This allows doctest to be used with namespace modules
1 parent 06738e3 commit c34eaaa

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

changelog/3396.improvement.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Doctests now respect the ``--import-mode`` flag.

src/_pytest/doctest.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,11 @@ def _find(
542542
)
543543
else:
544544
try:
545-
module = import_path(self.path, root=self.config.rootpath)
545+
module = import_path(
546+
self.path,
547+
root=self.config.rootpath,
548+
mode=self.config.getoption("importmode"),
549+
)
546550
except ImportError:
547551
if self.config.getvalue("doctest_ignore_import_errors"):
548552
pytest.skip("unable to import module %r" % self.path)

testing/test_doctest.py

+22
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,28 @@ def test_simple_doctestfile(self, pytester: Pytester):
113113
reprec = pytester.inline_run(p)
114114
reprec.assertoutcome(failed=1)
115115

116+
def test_importmode(self, pytester: Pytester):
117+
p = pytester.makepyfile(
118+
**{
119+
"namespacepkg/innerpkg/__init__.py": "",
120+
"namespacepkg/innerpkg/a.py": """
121+
def some_func():
122+
return 42
123+
""",
124+
"namespacepkg/innerpkg/b.py": """
125+
from namespacepkg.innerpkg.a import some_func
126+
def my_func():
127+
'''
128+
>>> my_func()
129+
42
130+
'''
131+
return some_func()
132+
""",
133+
}
134+
)
135+
reprec = pytester.inline_run(p, "--doctest-modules", "--import-mode=importlib")
136+
reprec.assertoutcome(passed=1)
137+
116138
def test_new_pattern(self, pytester: Pytester):
117139
p = pytester.maketxtfile(
118140
xdoc="""

0 commit comments

Comments
 (0)