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

ev-*_*-br 65 python pytest

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

语境:我期待评估从移植测试套件的难度nosepytest.该套件相当大,并且基于使用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

  • `addopts = -p no:warnings`确实是个坏主意,而CloC解决方案要好得多,但是当`ignore :: InsecureRequestWarning`无法识别时,我不得不使用您的,所以您也得+1 (2认同)

The*_*ler 59

来自pytest --help:

--disable-pytest-warnings
                      disable warnings summary, overrides -r w flag
Run Code Online (Sandbox Code Playgroud)

  • 正如帮助文本所说,这只会省略文本摘要.结果仍然是黄色并显示有警告. (4认同)
  • 最好放入 pytest.ini 文件中,如下所示。 (2认同)

Clo*_*loC 53

我想你不想隐藏所有警告,而只是隐藏那些不相关的警告.在这种情况下,从导入的python模块中删除警告.

阅读有关警告捕获的 pytest文档:

-W命令行选项和filterwarnings ini选项都基于Python自己的-W选项warnings.simplefilter,因此请参阅Python文档中的那些部分以获取其他示例和高级用法.

所以你可以用python的-W选项过滤警告!

它似乎pytest完全删除了过滤器,因为它DeprecationWarning在运行时显示了所有这些过滤器,而Python关于默认警告过滤器的文档明确指出:

在常规发布版本中,默认警告筛选器具有以下条目(按优先顺序排列):

default::DeprecationWarning:__main__
ignore::DeprecationWarning
ignore::PendingDeprecationWarning
ignore::ImportWarning
ignore::ResourceWarning
Run Code Online (Sandbox Code Playgroud)

因此,在您的情况下,如果您想要过滤掉要忽略的警告类型(例如那些)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)

  • 要过滤模块,请使用正则表达式.忽略所有sqlalchemy模块的弃用警告的示例:`ignore :: DeprecationWarning:sqlalchemy.*:` (5认同)
  • @colini 作为 -W 标志的参数对我来说失败了,但它在我的 pytest.ini 文件中对我有用。 (2认同)
  • 在 pytest 6.1 中,尾随正则表达式对我不起作用,但您可以在中间插入正则表达式以与警告消息的开头匹配。`ignore:.*U.*mode is deprecated:DeprecationWarning` 会忽略所有 DeprecationWarning 类型的警告,其中消息开头与正则表达式“.*U.*mode is deprecated”匹配。[更多信息](https://docs.pytest.org/en/stable/warnings.html#deprecationwarning-and-pendingdeprecationwarning) (2认同)
  • @WhiteHotLoveTiger 与 `ignore::DeprecationWarning:tensorflow[.*]` 相同 - 仅适用于配置文件,提交了错误报告:https://github.com/pytest-dev/pytest/issues/8751 (2认同)

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

  • `filterwarnings = ["ignore::DeprecationWarning"]` 忽略这种情况下的所有已弃用的警告 (2认同)

Pol*_*olv 9

我不想隐藏所有警告,所以我把它放进去 pytest.ini

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