仅抑制其他人代码的 pytest 警告

oiv*_*vio 6 pytest

我是pytest第一次尝试。我如何抑制发出的关于我的代码所依赖的其他人的代码的警告而不抑制关于我自己的代码的警告?

现在我有这个,pytest.ini所以我不必看到 pytest 警告我关于jsonschema我正在使用的包的一些弃用。

[pytest]
filterwarnings =
    ignore::DeprecationWarning
Run Code Online (Sandbox Code Playgroud)

但是现在,如果我在自己的代码中编写任何应该触发弃用警告的内容,我就会错过它。

Sil*_*Guy 7

pytest-warning 的语法是action:message:category:module:lineno. 您可以使用此配置仅忽略 jsonschema:

[pytest]
filterwarnings =
    ignore::DeprecationWarning:jsonschema
Run Code Online (Sandbox Code Playgroud)

您还可以在这些字段中使用正则表达式。如果您想排除除您之外的所有警告:

[pytest]
filterwarnings =
    ignore::DeprecationWarning:!yourtestmodule
Run Code Online (Sandbox Code Playgroud)

Pytest 使用与 python 相同的过滤器警告。您可以在此处了解有关 Python 警告的更多信息:https : //docs.python.org/3/library/warnings.html#warning-filter

来源:https://github.com/fschulze/pytest-warnings/blob/master/pytest_warnings/初始化的.py#L18

  • 这似乎不再可以了,这里正确的用法是“ignore::DeprecationWarning:(?!yourtestmodule)”,因为最后一个值是正则表达式 (2认同)