【问题标题】:How to disable internal pytest warnings?如何禁用内部 pytest 警告?
【发布时间】: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 ======================

【问题讨论】:

    标签: python warnings pytest


    【解决方案1】:

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

    [pytest]
    filterwarnings =
        ignore::pytest.PytestCacheWarning
    

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

    [pytest]
    filterwarnings =
        ignore::pytest.PytestWarning
    

    【讨论】:

    • 感谢您的回答,但还是不行。我稍微改变了我的问题以包含可重现的示例,并从 PytestAssertRewriteWarning 切换到 PytestCacheWarning
    • 谢谢,通读the docs 看来我的答案应该是可行的,所以这可能是 pytest 的问题。我建议在 pytest github 上提交问题。
    猜你喜欢
    • 2022-11-28
    • 2013-01-05
    • 2012-08-03
    • 2011-05-19
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多