From 8b48621687f1a4bbc4832d1c2b852bec061dbfff Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Fri, 4 Jan 2019 17:56:13 +0000 Subject: [PATCH] Allow providing a custom reason for `importorskip` --- src/_pytest/outcomes.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/_pytest/outcomes.py b/src/_pytest/outcomes.py index 714be3088..cdda8630e 100644 --- a/src/_pytest/outcomes.py +++ b/src/_pytest/outcomes.py @@ -137,7 +137,7 @@ def xfail(reason=""): xfail.Exception = XFailed -def importorskip(modname, minversion=None): +def importorskip(modname, minversion=None, reason=None): """ return imported module if it has at least "minversion" as its __version__ attribute. If no minversion is specified the a skip is only triggered if the module can not be imported. @@ -159,7 +159,9 @@ def importorskip(modname, minversion=None): # Do not raise chained exception here(#1485) should_skip = True if should_skip: - raise Skipped("could not import %r" % (modname,), allow_module_level=True) + if reason is None: + reason = "could not import %r" % (modname,) + raise Skipped(reason, allow_module_level=True) mod = sys.modules[modname] if minversion is None: return mod