Added warning and Test Cases

This commit is contained in:
shekhuverma 2024-04-20 12:43:16 +05:30
parent 543262c4c8
commit 9a9643d29e
2 changed files with 18 additions and 8 deletions

View File

@ -232,20 +232,25 @@ def importorskip(
warnings.simplefilter("ignore") warnings.simplefilter("ignore")
if exc_type is None: if exc_type is None:
exc_type = ModuleNotFoundError
else:
exc_type = ImportError exc_type = ImportError
warnings.warn( warn_on_import_error = True
PytestDeprecationWarning( else:
"The Default behaviour will change to ImportError in future", warn_on_import_error = False
)
)
try: try:
__import__(modname) __import__(modname)
except exc_type as exc: except exc_type as exc:
if reason is None: if reason is None:
reason = f"could not import {modname!r}: {exc}" reason = f"could not import {modname!r}: {exc}"
if warn_on_import_error and type(exc) is ImportError:
warnings.warn(
PytestDeprecationWarning(
f"""pytest.importorskip() caught {exc},but this will change in a future pytest release
to only capture ModuleNotFoundError exceptions by default.\nTo overwrite the future
behavior and silence this warning, pass exc_type=ImportError explicitly."""
)
)
raise Skipped(reason, allow_module_level=True) from None raise Skipped(reason, allow_module_level=True) from None
mod = sys.modules[modname] mod = sys.modules[modname]

View File

@ -774,7 +774,12 @@ def test_importorskip_importError_warning(pytester: Pytester) -> None:
with pytest.raises(pytest.skip.Exception): with pytest.raises(pytest.skip.Exception):
with pytest.warns(PytestDeprecationWarning): with pytest.warns(PytestDeprecationWarning):
pytest.importorskip(fn.stem, exc_type=ImportError) pytest.importorskip(fn.stem)
def test_importorskip_ModuleNotFoundError() -> None:
with pytest.raises(pytest.skip.Exception):
pytest.importorskip("abcdefgh")
def test_importorskip_dev_module(monkeypatch) -> None: def test_importorskip_dev_module(monkeypatch) -> None: