pytest: Some internal warnings from pytest cannot be filtered with settings in pytest.ini

I want to disable PytestCacheWarning with the following pytest.ini file:

[pytest]
filterwarnings =
    ignore::pytest.PytestCacheWarning

But the warnings are still shown in report summary. Minimal reproducible example:

  1. pytest.ini file as shown above
  2. empty directory .pytest_cache with 444 permissions
  3. running pytest gives the following:
~/repos/reproduce_pytest_bug$ pytest
============= test session starts =================
platform linux -- Python 3.7.6, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /home/sanyash/repos/reproduce_pytest_bug, inifile: pytest.ini
plugins: celery-4.4.0, aiohttp-0.3.0
collected 0 items                                                                                  

============= warnings summary ================
/home/sanyash/.local/lib/python3.7/site-packages/_pytest/cacheprovider.py:137
  /home/sanyash/.local/lib/python3.7/site-packages/_pytest/cacheprovider.py:137: PytestCacheWarning: could not create cache path /home/sanyash/repos/reproduce_pytest_bug/.pytest_cache/v/cache/stepwise
    self.warn("could not create cache path {path}", path=path)

/home/sanyash/.local/lib/python3.7/site-packages/_pytest/cacheprovider.py:137
  /home/sanyash/.local/lib/python3.7/site-packages/_pytest/cacheprovider.py:137: PytestCacheWarning: could not create cache path /home/sanyash/repos/reproduce_pytest_bug/.pytest_cache/v/cache/nodeids
    self.warn("could not create cache path {path}", path=path)

-- Docs: https://docs.pytest.org/en/latest/warnings.html
============= 2 warnings in 0.00s =============

versions:

  • Ubuntu 18.04
  • Python 3.7.6
  • pytest 5.3.5

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (11 by maintainers)

Commits related to this issue

Most upvoted comments

@blueyed there are at least two cases:

  1. I run pytest inside docker container and if I don’t specify a writable bind for .pytest_cache directory, I get PytestCacheWarning because pytest is not able to write a cache.
  2. I import conftest_additional.py inside main conftest.py and thus get PytestAssertRewriteWarning: Module already imported so cannot be rewritten: conftest_additional. I dont know yet how to work around it.

I believe that every such case can be handled in proper way, but the simplest is just disabling all internal warnings because I dont care much about them (I can be wrong here and maybe I should care, but they really don’t seem critical)

I started seeing this too with pytest 6.0.0 that also cannot be ignored with filterwarnings:

    self._warn_or_fail_if_strict("Unknown config ini key: {}\n".format(key))
.../python3.7/site-packages/_pytest/config/__init__.py:1148
  .../python3.7/site-packages/_pytest/config/__init__.py:1148: PytestConfigWarning: Unknown config ini key: qt_no_exception_capture

The config is added just in case someone has pytest-qt installed but it is not installed in any circumstances in the project itself. So we know it is not used and we just want to silence the warning. Thanks.