MCPcopy Create free account
hub / github.com/pytest-dev/pytest / test_importorskip

Function test_importorskip

testing/test_runner.py:698–728  ·  view source on GitHub ↗
(monkeypatch)

Source from the content-addressed store, hash-verified

696
697
698def test_importorskip(monkeypatch) -> None:
699 importorskip = pytest.importorskip
700
701 def f():
702 importorskip("asdlkj")
703
704 try:
705 sysmod = importorskip("sys")
706 assert sysmod is sys
707 # path = pytest.importorskip("os.path")
708 # assert path == os.path
709 excinfo = pytest.raises(pytest.skip.Exception, f)
710 assert excinfo is not None
711 excrepr = excinfo.getrepr()
712 assert excrepr is not None
713 assert excrepr.reprcrash is not None
714 path = Path(excrepr.reprcrash.path)
715 # check that importorskip reports the actual call
716 # in this test the test_runner.py file
717 assert path.stem == "test_runner"
718 pytest.raises(SyntaxError, pytest.importorskip, "x y z")
719 pytest.raises(SyntaxError, pytest.importorskip, "x=y")
720 mod = types.ModuleType("hello123")
721 mod.__version__ = "1.3" # type: ignore
722 monkeypatch.setitem(sys.modules, "hello123", mod)
723 with pytest.raises(pytest.skip.Exception):
724 pytest.importorskip("hello123", minversion="1.3.1")
725 mod2 = pytest.importorskip("hello123", minversion="1.3")
726 assert mod2 == mod
727 except pytest.skip.Exception: # pragma: no cover
728 assert False, f"spurious skip: {ExceptionInfo.from_current()}"
729
730
731def test_importorskip_imports_last_module_part() -> None:

Callers

nothing calls this directly

Calls 4

importorskipFunction · 0.85
getreprMethod · 0.80
setitemMethod · 0.80
from_currentMethod · 0.80

Tested by

no test coverage detected