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

Function test_importorskip

testing/test_runner.py:794–826  ·  testing/test_runner.py::test_importorskip
(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(class="st">"asdlkj")
799
800 try:
801 sysmod = importorskip(class="st">"sys")
802 assert sysmod is sys
803 class="cm"># path = pytest.importorskip(class="st">"os.path")
804 class="cm"># 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 class="cm"># check that importorskip reports the actual call
812 class="cm"># in this test the test_runner.py file
813 assert path.stem == class="st">"test_runner"
814 with pytest.raises(SyntaxError):
815 pytest.importorskip(class="st">"x y z")
816 with pytest.raises(SyntaxError):
817 pytest.importorskip(class="st">"x=y")
818 mod = types.ModuleType(class="st">"hello123")
819 mod.__version__ = class="st">"1.3" class="cm"># type: ignore
820 monkeypatch.setitem(sys.modules, class="st">"hello123", mod)
821 with pytest.raises(pytest.skip.Exception):
822 pytest.importorskip(class="st">"hello123", minversion=class="st">"1.3.1")
823 mod2 = pytest.importorskip(class="st">"hello123", minversion=class="st">"1.3")
824 assert mod2 == mod
825 except pytest.skip.Exception: class="cm"># pragma: no cover
826 assert False, fclass="st">"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