【发布时间】:2020-05-21 06:59:43
【问题描述】:
我想禁用 pytest.ini 中的所有 pytest 内部警告,例如 PytestCacheWarning,但目前没有运气。以下 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 ======================
【问题讨论】: