仅抑制其他人代码的 pytest 警告 [英] Suppress pytest warnings for other peoples code only
问题描述
我是第一次尝试 pytest
.如何在不抑制关于我自己代码的警告的情况下抑制针对我的代码所依赖的其他人的代码发出的警告?
I'm trying out pytest
for the first time. How do I suppress warnings issued about other peoples code that my code depends on without suppressing warnings about my own code?
现在我在我的 pytest.ini
中有这个,所以我不必看到 pytest 警告我关于我正在使用的 jsonschema
包的一些弃用.
Right now I have this in my pytest.ini
so I don't have to see pytest warn me about some deprecation on the jsonschema
package that I'm using.
[pytest]
filterwarnings =
ignore::DeprecationWarning
但是现在,如果我在自己的代码中编写任何应该触发弃用警告的内容,我就会错过它.
But now if I write anything in my own code that should fire of a deprecation warning I'll miss it.
推荐答案
pytest-warning 的语法是 action:message:category:module:lineno
.您可以使用此配置仅忽略 jsonschema:
The syntax for pytest-warning is action:message:category:module:lineno
. You can use this config for ignoring only jsonschema:
[pytest]
filterwarnings =
ignore::DeprecationWarning:jsonschema
您也可以在这些字段中使用正则表达式.如果您想排除除您之外的所有警告:
You can also use regex in those fields. If you want to exclude all warnings except yours:
[pytest]
filterwarnings =
ignore::DeprecationWarning:!yourtestmodule
Pytest 使用与 python 相同的过滤器警告.您可以在此处了解有关 Python 警告的更多信息:https://docs.python.org/3/library/warnings.html#warning-filter
Pytest uses the same filterwarning as python. You can learn more about python warnings here: https://docs.python.org/3/library/warnings.html#warning-filter
来源:https://github.com/fschulze/pytest-warnings/blob/master/pytest_warnings/init.py#L18
Source: https://github.com/fschulze/pytest-warnings/blob/master/pytest_warnings/init.py#L18
这篇关于仅抑制其他人代码的 pytest 警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!