Skip to content

Commit 7dcabc1

Browse files
authored
Merge pull request #10088 from alicederyn/doctest.importmode
Pass importmode to import_path in DoctestModule
2 parents 2941da0 + 85000f0 commit 7dcabc1

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Alan Velasco
1515
Alexander Johnson
1616
Alexander King
1717
Alexei Kozlenok
18+
Alice Purcell
1819
Allan Feldman
1920
Aly Sivji
2021
Amir Elkess

changelog/3396.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Doctests now respect the ``--import-mode`` flag.

src/_pytest/doctest.py

Lines changed: 5 additions & 1 deletion
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

Lines changed: 22 additions & 0 deletions
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)