From 9a9643d29e12f8d7f2153c0aa174f3ca75445c5c Mon Sep 17 00:00:00 2001 From: shekhuverma Date: Sat, 20 Apr 2024 12:43:16 +0530 Subject: [PATCH] Added warning and Test Cases --- src/_pytest/outcomes.py | 19 ++++++++++++------- testing/test_runner.py | 7 ++++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/_pytest/outcomes.py b/src/_pytest/outcomes.py index 0e4e5f566..8f19f29e1 100644 --- a/src/_pytest/outcomes.py +++ b/src/_pytest/outcomes.py @@ -232,20 +232,25 @@ def importorskip( warnings.simplefilter("ignore") if exc_type is None: - exc_type = ModuleNotFoundError - else: exc_type = ImportError - warnings.warn( - PytestDeprecationWarning( - "The Default behaviour will change to ImportError in future", - ) - ) + warn_on_import_error = True + else: + warn_on_import_error = False try: __import__(modname) except exc_type as exc: if reason is None: 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 mod = sys.modules[modname] diff --git a/testing/test_runner.py b/testing/test_runner.py index fd8d35187..f6ab5e450 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -774,7 +774,12 @@ def test_importorskip_importError_warning(pytester: Pytester) -> None: with pytest.raises(pytest.skip.Exception): 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: