From fd288e13ac8681fc7c61691748e858785302f0d9 Mon Sep 17 00:00:00 2001 From: shekhuverma Date: Fri, 19 Apr 2024 16:43:07 +0530 Subject: [PATCH] added testing for importorskip --- testing/test_runner.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/testing/test_runner.py b/testing/test_runner.py index 8cc496f70..02cfbb2c5 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -1,4 +1,5 @@ # mypy: allow-untyped-defs +import builtins from functools import partial import inspect import os @@ -7,6 +8,8 @@ import sys import types from typing import Dict from typing import List +from typing import Mapping +from typing import Sequence from typing import Tuple from typing import Type @@ -762,6 +765,27 @@ def test_importorskip_imports_last_module_part() -> None: assert os.path == ospath +def test_importorskip_importError_Exception() -> None: + ## Mocking the import function to raise a importError + realimport = builtins.__import__ + + def myimport( + name: str, + globals: Mapping[str, object] | None = None, + locals: Mapping[str, object] | None = None, + fromlist: Sequence[str] = (), + level: int = 0, + ) -> types.ModuleType: + raise ImportError + + builtins.__import__ = myimport + + with pytest.raises(ImportError): + pytest.importorskip("abcdefghi") + + builtins.__import__ = realimport + + def test_importorskip_dev_module(monkeypatch) -> None: try: mod = types.ModuleType("mockmodule")