diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index 2f7f8845440..9f94605d4df 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -71,10 +71,10 @@ def cache_dir_from_config(config): return resolve_from_str(config.getini("cache_dir"), config.rootdir) def warn(self, fmt, **args): - from _pytest.warnings import _issue_warning_captured + import warnings from _pytest.warning_types import PytestCacheWarning - _issue_warning_captured( + warnings.warn( PytestCacheWarning(fmt.format(**args) if args else fmt), self._config.hook, stacklevel=3, diff --git a/src/_pytest/warnings.py b/src/_pytest/warnings.py index 18e4def2116..2a4d189d573 100644 --- a/src/_pytest/warnings.py +++ b/src/_pytest/warnings.py @@ -136,6 +136,15 @@ def pytest_terminal_summary(terminalreporter): yield +@pytest.hookimpl(hookwrapper=True) +def pytest_sessionfinish(session): + config = session.config + with catch_warnings_for_item( + config=config, ihook=config.hook, when="config", item=None + ): + yield + + def _issue_warning_captured(warning, hook, stacklevel): """ This function should be used instead of calling ``warnings.warn`` directly when we are in the "configure" stage: diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py index d37f18f0ff5..6dd987b613e 100644 --- a/testing/test_cacheprovider.py +++ b/testing/test_cacheprovider.py @@ -56,9 +56,7 @@ def test_cache_writefail_permissions(self, testdir): testdir.tmpdir.ensure_dir(".pytest_cache").chmod(mode) @pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows") - @pytest.mark.filterwarnings( - "ignore:could not create cache path:pytest.PytestWarning" - ) + @pytest.mark.filterwarnings("default") def test_cache_failure_warns(self, testdir, monkeypatch): monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1") cache_dir = str(testdir.tmpdir.ensure_dir(".pytest_cache"))