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

Created on 6 Feb 2020  路  15Comments  路  Source: pytest-dev/pytest

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
warnings config bug

All 15 comments

i suspect the cache plugin is initialized before the warnings plugin

Thanks for quick response. I hope it can be fixed for all PytestWarnings, not only PytestCacheWarning, because I also faced PytestRewriteAssertWarning and want to disable it too, probably with ignore::pytest.PytestWarning for all warnings.

@sanyassh can you try moving the warning plugin up there?

@blueyed placing warnings before cacheprovider or before any other plugin didn't help.

Yeah it is not only the order of installation, I'm afraid we might need to duplicate the warning filtering mechanism to properly filter warnings emitted using _issue_warning_captured.

I think _issue_warning_captured can be removed almost everywhere, and is certainly wrongly used with that warning (IIRC, I've looked into that a few days ago).
I.e. the easy fix here is to use warnings for those warnings (pytest.PytestCacheWarning).
But I have more in a stash in this regard, e.g. with regard to fixing/improving the tests also.

I.e. the easy fix here is to use warnings for those warnings (pytest.PytestCacheWarning).

IIRC I explicitly added _issue_warning_captured becuase when those warnings were issued the warnings capture mechanism was not installed yet. But of course if we can remove it now and I'm misremembering things all the better. 馃憤

It's not trivial to remove it everywhere, but with the warning here it is not necessary to use it. Better to be seen with actual code though probably.. ;)

It's not trivial to remove it everywhere, but with the warning here it is not necessary to use it.

Not sure what you meant, but yeah, with the code is probably easier to discuss/understand.

@sanyassh
Why do you want to filter them in the first place btw?

@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 have PytestAssertRewriteWarning warning that I am trying to ignore, even disabling all warnings

pytest  -p no:warnings

Still produce this warning

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.

Was this page helpful?
0 / 5 - 0 ratings