importorskip: display/include ImportError (#5269)
importorskip: display/include ImportError
This commit is contained in:
commit
b900b4155f
|
@ -0,0 +1 @@
|
||||||
|
``pytest.importorskip`` includes the ``ImportError`` now in the default ``reason``.
|
|
@ -154,7 +154,7 @@ def importorskip(modname, minversion=None, reason=None):
|
||||||
|
|
||||||
__tracebackhide__ = True
|
__tracebackhide__ = True
|
||||||
compile(modname, "", "eval") # to catch syntaxerrors
|
compile(modname, "", "eval") # to catch syntaxerrors
|
||||||
should_skip = False
|
import_exc = None
|
||||||
|
|
||||||
with warnings.catch_warnings():
|
with warnings.catch_warnings():
|
||||||
# make sure to ignore ImportWarnings that might happen because
|
# make sure to ignore ImportWarnings that might happen because
|
||||||
|
@ -163,12 +163,12 @@ def importorskip(modname, minversion=None, reason=None):
|
||||||
warnings.simplefilter("ignore")
|
warnings.simplefilter("ignore")
|
||||||
try:
|
try:
|
||||||
__import__(modname)
|
__import__(modname)
|
||||||
except ImportError:
|
except ImportError as exc:
|
||||||
# Do not raise chained exception here(#1485)
|
# Do not raise chained exception here(#1485)
|
||||||
should_skip = True
|
import_exc = exc
|
||||||
if should_skip:
|
if import_exc:
|
||||||
if reason is None:
|
if reason is None:
|
||||||
reason = "could not import %r" % (modname,)
|
reason = "could not import %r: %s" % (modname, import_exc)
|
||||||
raise Skipped(reason, allow_module_level=True)
|
raise Skipped(reason, allow_module_level=True)
|
||||||
mod = sys.modules[modname]
|
mod = sys.modules[modname]
|
||||||
if minversion is None:
|
if minversion is None:
|
||||||
|
|
|
@ -1177,3 +1177,11 @@ def test_summary_list_after_errors(testdir):
|
||||||
"FAILED test_summary_list_after_errors.py::test_fail - assert 0",
|
"FAILED test_summary_list_after_errors.py::test_fail - assert 0",
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_importorskip():
|
||||||
|
with pytest.raises(
|
||||||
|
pytest.skip.Exception,
|
||||||
|
match="^could not import 'doesnotexist': No module named .*",
|
||||||
|
):
|
||||||
|
pytest.importorskip("doesnotexist")
|
||||||
|
|
Loading…
Reference in New Issue