Как отключить внутренние предупреждения Pytest? - PullRequest
2 голосов
/ 05 февраля 2020

Я хочу отключить все внутренние предупреждения Pytest, такие как PytestCacheWarning в pytest.ini, но в настоящее время мне не повезло с этим. Следующий ini-файл не работает так, как я ожидаю:

[pytest]
filterwarnings:
    ignore::pytest.PytestCacheWarning

Как правильно это сделать? Примечание: я не хочу отключать все предупреждения, только те, которые определены внутри реализации pytest.


Пример минимальной воспроизводимости:

1) Создайте следующую структуру:

some_dir/
    .pytest_cache/
    test_something.py
    pytest.ini

2) Поместите это в test_something.py файл:

def test_something():
    assert False

3) Поместите это в pytest.ini файл:

[pytest]
filterwarnings:
    ignore::pytest.PytestCacheWarning

4) сделайте chmod 444 .pytest_cache, чтобы прокудировать PytestCacheWarning: could not create cache path предупреждение

5) пробег 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 1 item                                                         

test_something.py F                                                [100%]

================================ FAILURES ================================
_____________________________ test_something _____________________________

    def test_something():
>       assert False
E       assert False

test_something.py:2: AssertionError
============================ 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)

/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/lastfailed
    self.warn("could not create cache path {path}", path=path)

-- Docs: https://docs.pytest.org/en/latest/warnings.html
===================== 1 failed, 3 warnings in 0.03s ======================

1 Ответ

0 голосов
/ 05 февраля 2020

Вы должны использовать путь импорта, чтобы игнорировать его:

[pytest]
filterwarnings =
    ignore::pytest.PytestCacheWarning

, поэтому для всех предупреждений о pytest вы должны использовать общий базовый класс:

[pytest]
filterwarnings =
    ignore::pytest.PytestWarning
...