Skip to content

Commit 4cb058c

Browse files
committed
BUG: fix __doctest_requires__ when constraint is present
1 parent d51d45a commit 4cb058c

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

pytest_doctestplus/plugin.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ def conditionally_insert_skip(test):
874874
continue # The pattern does not apply
875875

876876
for mod in mods:
877-
self._prepend_importorskip(test, module=mod)
877+
self._prepend_module_check(test, module=mod)
878878
return True
879879

880880
for _test in tests:
@@ -893,17 +893,20 @@ def _prepend_skip(self, test):
893893
importorskip = doctest.Example(source=source, want="")
894894
test.examples.insert(0, importorskip)
895895

896-
def _prepend_importorskip(self, test, *, module):
897-
"""Prepends `pytest.importorskip` before the doctest."""
896+
def _prepend_module_check(self, test, *, module):
897+
"""Prepends module checker before the doctest."""
898+
escaped_module = module.replace("'", "\\'")
898899
source = (
900+
"from pytest_doctestplus.utils import ModuleChecker; "
899901
"import pytest; "
900902
# Hide output of this statement in `___`, otherwise doctests fail
901-
f"___ = pytest.importorskip({module!r}); "
903+
f"___ = ModuleChecker().check('{escaped_module}') or "
904+
f"pytest.skip('could not import {escaped_module}'); "
902905
# Don't impact what's available in the namespace
903-
"del pytest; del ___"
906+
"del ModuleChecker, pytest, ___"
904907
)
905-
importorskip = doctest.Example(source=source, want="")
906-
test.examples.insert(0, importorskip)
908+
module_check = doctest.Example(source=source, want="")
909+
test.examples.insert(0, module_check)
907910

908911

909912
def write_modified_file(fname, new_fname, changes, encoding=None):

0 commit comments

Comments
 (0)