MCPcopy
hub / github.com/pytest-dev/pytest / test_importorskip

Function test_importorskip

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

Source from the content-addressed store, hash-verified

792
793
794def test_importorskip(monkeypatch) -> None:
795 importorskip = pytest.importorskip
796
797 def f():
798 importorskip("asdlkj")
799
800 try:
801 sysmod = importorskip("sys")
802 assert sysmod is sys
803 # path = pytest.importorskip("os.path")
804 # assert path == os.path
805 excinfo = pytest.raises(pytest.skip.Exception, f)
806 assert excinfo is not None
807 excrepr = excinfo.getrepr()
808 assert excrepr is not None
809 assert excrepr.reprcrash is not None
810 path = Path(excrepr.reprcrash.path)
811 # check that importorskip reports the actual call
812 # in this test the test_runner.py file
813 assert path.stem == "test_runner"
814 with pytest.raises(SyntaxError):
815 pytest.importorskip("x y z")
816 with pytest.raises(SyntaxError):
817 pytest.importorskip("x=y")
818 mod = types.ModuleType("hello123")
819 mod.__version__ = "1.3" # type: ignore
820 monkeypatch.setitem(sys.modules, "hello123", mod)
821 with pytest.raises(pytest.skip.Exception):
822 pytest.importorskip("hello123", minversion="1.3.1")
823 mod2 = pytest.importorskip("hello123", minversion="1.3")
824 assert mod2 == mod
825 except pytest.skip.Exception: # pragma: no cover
826 assert False, f"spurious skip: {ExceptionInfo.from_current()}"
827
828
829def 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

Used in the wild real call sites across dependent graphs

searching dependent graphs…