有没有办法抑制pytest的内部弃用警告?
语境:我期待评估从移植测试套件的难度nose来pytest.该套件相当大,并且基于使用nose风格yield的测试生成器.
我想首先确保现有测试通过pytest,然后可能将测试生成器更改为parameterized.
刚运行$ pytest path-to-test-folderpytest 3.0.4完全由页面和页面控制
WC1 ~repos/numpy/numpy/lib/tests/test_twodim_base.py yield tests are deprecated, and scheduled to be removed in pytest 4.0
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这些警告?
Bla*_*ise 67
pytest -p no:warnings
或者将以下内容添加到pytest.ini或tox.ini:
[pytest]
addopts = -p no:warnings
Run Code Online (Sandbox Code Playgroud)
结果将是绿色,没有任何警告指示(除非测试当然失败).
请参阅https://docs.pytest.org/en/latest/warnings.html#disabling-warning-capture
The*_*ler 59
来自pytest --help:
--disable-pytest-warnings
disable warnings summary, overrides -r w flag
Run Code Online (Sandbox Code Playgroud)
Clo*_*loC 53
我想你不想隐藏所有警告,而只是隐藏那些不相关的警告.在这种情况下,从导入的python模块中删除警告.
阅读有关警告捕获的 pytest文档:
-W命令行选项和filterwarnings ini选项都基于Python自己的-W选项和warnings.simplefilter,因此请参阅Python文档中的那些部分以获取其他示例和高级用法.
所以你可以用python的-W选项过滤警告!
它似乎pytest完全删除了过滤器,因为它DeprecationWarning在运行时显示了所有这些过滤器,而Python关于默认警告过滤器的文档明确指出:
在常规发布版本中,默认警告筛选器具有以下条目(按优先顺序排列):
Run Code Online (Sandbox Code Playgroud)default::DeprecationWarning:__main__ ignore::DeprecationWarning ignore::PendingDeprecationWarning ignore::ImportWarning ignore::ResourceWarning
因此,在您的情况下,如果您想要过滤掉要忽略的警告类型(例如那些)DeprecationWarning,只需运行带有-W选项的pytest命令:
$ pytest path-to-test-folder -W ignore::DeprecationWarning
Run Code Online (Sandbox Code Playgroud)
编辑:从colini的评论,可以按模块过滤.忽略所有sqlalchemy的弃用警告的示例:
ignore::DeprecationWarning:sqlalchemy.*:
Run Code Online (Sandbox Code Playgroud)
然后,您可以列出已安装的模块,这些模块会在输出中产生过多噪音 pytest
与文件而不是命令行一起使用:
您可能更喜欢在pytest.ini文件中列出这些过滤器:
[pytest]
filterwarnings =
ignore::DeprecationWarning
Run Code Online (Sandbox Code Playgroud)
pk7*_*786 18
在 pytest.ini 文件中,您可以添加:
[pytest]
addopts = -p no:warnings
Run Code Online (Sandbox Code Playgroud)
或在命令行中传递以下行。如果您的测试套件使用外部系统处理警告,这可能很有用。
-p 否:警告
或者,如果您只想隐藏一些特定的已弃用警告,请在 pytest.ini 文件中添加以下语句
[pytest]
filterwarnings =
ignore:.*U.*mode is deprecated:DeprecationWarning
Run Code Online (Sandbox Code Playgroud)
这将忽略所有类型为 DeprecationWarning 的警告,其中消息的开头与正则表达式“.*U.*mode is deprecated”相匹配。
或 虽然不推荐,但您可以使用
--disable-warnings
命令行选项以完全从测试运行输出中抑制警告摘要。
Vla*_*den 11
以下是有关如何在使用文件作为配置时抑制警告的链接pyproject.toml。
[tool.pytest.ini_options]
testpaths = ["./tests/unit"]
filterwarnings = ["ignore:::.*third_party_package.module:123", "ignore:::.*another_module*"]
Run Code Online (Sandbox Code Playgroud)
123在本例中是要抑制的行号
有关编写警告过滤器的一般方法的更多信息,请参阅 python 官方文档“警告过滤器”和“描述警告过滤器”。简而言之,各个警告过滤器被指定为一系列用冒号分隔的字段:action:message:category:module:line
我不想隐藏所有警告,所以我把它放进去 pytest.ini
[pytest]
filterwarnings =
ignore::DeprecationWarning
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23031 次 |
| 最近记录: |