gpt4 book ai didi

python - 如何抑制 py.test 内部弃用警告

转载 作者:IT老高 更新时间:2023-10-28 20:23:30 25 4
gpt4 key购买 nike

有没有办法抑制 pytest 的内部弃用警告?

上下文:我正在评估将测试套件从 nose 移植到 pytest 的难度。该套件相当大,并且大量使用基于 nose 风格的 yield 的测试生成器。

我想首先确保 现有 测试通过 pytest,然后可能将测试生成器更改为 parameterized

仅仅用pytest 3.0.4运行$ pytest path-to-test-folder完全被页面和页面所支配

WC1 ~repos/numpy/numpy/lib/tests/test_twodim_base.py yield tests are deprecated, and scheduled to be removed in pytest 4.0

有没有办法关闭这些警告?

最佳答案

我认为您不想隐藏所有警告,而只想隐藏那些不相关的警告。在这种情况下,来自导入的 python 模块的弃用警告。

阅读有关 Warnings Capture 的 pytest 文档:

Both -W command-line option and filterwarnings ini option are based on Python’s own -W option and warnings.simplefilter, so please refer to those sections in the Python documentation for other examples and advanced usage.

因此您可以使用 python 的 -W 选项过滤警告!

似乎 pytest 完全删除了过滤器,因为它在运行时显示所有那些 DeprecationWarning,以及 Python 的关于 Default Warning Filters 的文档清楚地说:

In regular release builds, the default warning filter has thefollowing entries (in order of precedence):

default::DeprecationWarning:__main__
ignore::DeprecationWarning
ignore::PendingDeprecationWarning
ignore::ImportWarning
ignore::ResourceWarning

因此,在您的情况下,如果您想过滤您想要忽略的警告类型,例如那些 DeprecationWarning,只需使用 -W 运行 pytest 命令选项:

$ pytest path-to-test-folder -W ignore::DeprecationWarning

编辑:来自 colini的评论,可以按模块过滤。忽略所有 sqlalchemy 的弃用警告的示例:

ignore::DeprecationWarning:sqlalchemy.*:

然后您可以列出在 pytest

的输出中产生过多噪音的已安装模块

与文件一起使用而不是在命令行中使用:

您可能更喜欢在 pytest.ini 文件中列出这些过滤器:

[pytest]
filterwarnings =
ignore::DeprecationWarning

关于python - 如何抑制 py.test 内部弃用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40710094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com