Merge 662fc9c012
into c0dfc45186
This commit is contained in:
commit
f031ad0f12
|
@ -197,6 +197,14 @@ the test. You can also skip based on the version number of a library:
|
|||
The version will be read from the specified
|
||||
module's ``__version__`` attribute.
|
||||
|
||||
Sometimes importing a module can fail due to an exception, if you want to
|
||||
only skip if the module does not exist pass ModuleNotFoundError as the
|
||||
``exc`` kwarg:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
docutils = pytest.importorskip("docutils", exc=ModuleNotFoundError)
|
||||
|
||||
Summary
|
||||
~~~~~~~
|
||||
|
||||
|
|
|
@ -191,7 +191,11 @@ def xfail(reason: str = "") -> NoReturn:
|
|||
|
||||
|
||||
def importorskip(
|
||||
modname: str, minversion: Optional[str] = None, reason: Optional[str] = None
|
||||
modname: str,
|
||||
minversion: Optional[str] = None,
|
||||
reason: Optional[str] = None,
|
||||
*,
|
||||
exc: Type[ImportError] = ImportError,
|
||||
) -> Any:
|
||||
"""Import and return the requested module ``modname``, or skip the
|
||||
current test if the module cannot be imported.
|
||||
|
@ -204,6 +208,9 @@ def importorskip(
|
|||
:param reason:
|
||||
If given, this reason is shown as the message when the module cannot
|
||||
be imported.
|
||||
:param exc:
|
||||
Either ImportError or ModuleNotFoundError, the exception to catch
|
||||
when importing.
|
||||
|
||||
:returns:
|
||||
The imported module. This should be assigned to its canonical name.
|
||||
|
@ -224,9 +231,9 @@ def importorskip(
|
|||
warnings.simplefilter("ignore")
|
||||
try:
|
||||
__import__(modname)
|
||||
except ImportError as exc:
|
||||
except exc as e:
|
||||
if reason is None:
|
||||
reason = f"could not import {modname!r}: {exc}"
|
||||
reason = f"could not import {modname!r}: {e}"
|
||||
raise Skipped(reason, allow_module_level=True) from None
|
||||
mod = sys.modules[modname]
|
||||
if minversion is None:
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
# mypy: allow-untyped-defs
|
||||
import sys
|
||||
import textwrap
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from _pytest.monkeypatch import MonkeyPatch
|
||||
from _pytest.pytester import Pytester
|
||||
from _pytest.runner import runtestprotocol
|
||||
from _pytest.skipping import evaluate_skip_marks
|
||||
|
@ -1429,6 +1431,28 @@ def test_importorskip() -> None:
|
|||
pytest.importorskip("doesnotexist")
|
||||
|
||||
|
||||
def test_importorskip_module_not_found() -> None:
|
||||
with pytest.raises(
|
||||
pytest.skip.Exception,
|
||||
match="^could not import 'doesnotexist': No module named .*",
|
||||
):
|
||||
pytest.importorskip("doesnotexist", exc=ModuleNotFoundError)
|
||||
|
||||
|
||||
def test_importorskip_module_not_found_raises_on_import_error(
|
||||
monkeypatch: MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
on_path = tmp_path / "on_path"
|
||||
on_path.mkdir()
|
||||
|
||||
(on_path / "doesnotexist.py").write_bytes(b"1 / 0")
|
||||
|
||||
monkeypatch.syspath_prepend(on_path)
|
||||
|
||||
with pytest.raises(ImportError):
|
||||
pytest.importorskip("doesnotexist", exc=ModuleNotFoundError)
|
||||
|
||||
|
||||
def test_relpath_rootdir(pytester: Pytester) -> None:
|
||||
pytester.makepyfile(
|
||||
**{
|
||||
|
|
Loading…
Reference in New Issue