Skip to content

Commit

Permalink
Pass importmode to import_path in DoctestModule
Browse files Browse the repository at this point in the history
This allows doctest to be used with namespace modules
  • Loading branch information
alicederyn committed Jun 29, 2022
1 parent 06738e3 commit c34eaaa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/3396.improvement.rst
@@ -0,0 +1 @@
Doctests now respect the ``--import-mode`` flag.
6 changes: 5 additions & 1 deletion src/_pytest/doctest.py
Expand Up @@ -542,7 +542,11 @@ def _find(
)
else:
try:
module = import_path(self.path, root=self.config.rootpath)
module = import_path(
self.path,
root=self.config.rootpath,
mode=self.config.getoption("importmode"),
)
except ImportError:
if self.config.getvalue("doctest_ignore_import_errors"):
pytest.skip("unable to import module %r" % self.path)
Expand Down
22 changes: 22 additions & 0 deletions testing/test_doctest.py
Expand Up @@ -113,6 +113,28 @@ def test_simple_doctestfile(self, pytester: Pytester):
reprec = pytester.inline_run(p)
reprec.assertoutcome(failed=1)

def test_importmode(self, pytester: Pytester):
p = pytester.makepyfile(
**{
"namespacepkg/innerpkg/__init__.py": "",
"namespacepkg/innerpkg/a.py": """
def some_func():
return 42
""",
"namespacepkg/innerpkg/b.py": """
from namespacepkg.innerpkg.a import some_func
def my_func():
'''
>>> my_func()
42
'''
return some_func()
""",
}
)
reprec = pytester.inline_run(p, "--doctest-modules", "--import-mode=importlib")
reprec.assertoutcome(passed=1)

def test_new_pattern(self, pytester: Pytester):
p = pytester.maketxtfile(
xdoc="""
Expand Down

0 comments on commit c34eaaa

Please sign in to comment.