如何禁用内部pytest警告?

2024-10-17 08:20:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想禁用所有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)不要发出警告

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 ======================

Tags: pathpytestcachehomepytestlibpackages
1条回答
网友
1楼 · 发布于 2024-10-17 08:20:28

您必须使用导入路径来忽略它:

[pytest]
filterwarnings =
    ignore::pytest.PytestCacheWarning

因此,对于所有pytest警告,您将使用公共基类:

[pytest]
filterwarnings =
    ignore::pytest.PytestWarning

相关问题 更多 >