From c34eaaaa1c485e9c8c3d1fe7f05549b6436f49cc Mon Sep 17 00:00:00 2001 From: Alice Purcell Date: Wed, 29 Jun 2022 14:38:59 +0100 Subject: [PATCH 1/2] Pass importmode to import_path in DoctestModule This allows doctest to be used with namespace modules --- changelog/3396.improvement.rst | 1 + src/_pytest/doctest.py | 6 +++++- testing/test_doctest.py | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 changelog/3396.improvement.rst diff --git a/changelog/3396.improvement.rst b/changelog/3396.improvement.rst new file mode 100644 index 000000000..aa3f278c2 --- /dev/null +++ b/changelog/3396.improvement.rst @@ -0,0 +1 @@ +Doctests now respect the ``--import-mode`` flag. diff --git a/src/_pytest/doctest.py b/src/_pytest/doctest.py index ac5a29bb9..ed072fcd6 100644 --- a/src/_pytest/doctest.py +++ b/src/_pytest/doctest.py @@ -542,7 +542,11 @@ class DoctestModule(pytest.Module): ) 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) diff --git a/testing/test_doctest.py b/testing/test_doctest.py index 1b4809240..828253d32 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -113,6 +113,28 @@ class TestDoctests: 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=""" From 85000f037f33e34eb8b0a8d6430a9a33fbb1ee29 Mon Sep 17 00:00:00 2001 From: Alice Purcell Date: Wed, 29 Jun 2022 14:39:04 +0100 Subject: [PATCH 2/2] Added Alice Purcell to AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 2d3439b6c..8530e2e01 100644 --- a/AUTHORS +++ b/AUTHORS @@ -15,6 +15,7 @@ Alan Velasco Alexander Johnson Alexander King Alexei Kozlenok +Alice Purcell Allan Feldman Aly Sivji Amir Elkess