如何在运行单元测试时消除第三方库警告?

2024-06-13 11:28:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用PyScaffold设置了我的项目,在使用pytest运行单元测试时,我得到了以下第三方警告,我想摆脱,但不知道如何摆脱:

==================================== warnings summary ====================================
c:\dev\pyrepo\lib\site-packages\patsy\constraint.py:13
  c:\dev\pyrepo\lib\site-packages\patsy\constraint.py:13: DeprecationWarning: Using or importing
 the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in
 3.9 it will stop working
    from collections import Mapping

-- Docs: https://docs.pytest.org/en/latest/warnings.html

避免来自第三方库的警告的最好方法是什么,像这样,而不是我自己的项目代码警告


Tags: 项目frompydev警告pytestlibpackages
1条回答
网友
1楼 · 发布于 2024-06-13 11:28:05

抑制警告的方法有多种:

  • 使用命令行参数

要完全隐藏警告,请使用

pytest . -W ignore::DeprecationWarning

此命令将隐藏warnings summary,但将显示1 passed, 1 warning消息

pytest .  disable-warnings
  • 使用以下内容创建pytest.ini
[pytest]
filterwarnings =
    ignore::DeprecationWarning

您还可以使用正则表达式模式:

ignore:.*U.*mode is deprecated:DeprecationWarning

从文档中:

This will ignore all warnings of type DeprecationWarning where the start of the message matches the regular expression .*U.*mode is deprecated.

  • @pytest.mark.filterwarnings("ignore::DeprecationWarning")

  • 使用PYTHONWARNINGS环境变量

PYTHONWARNINGS="ignore::DeprecationWarning" pytest .

它的语法与-W命令行arg相同。更多here

更多详细信息请参见pytest docs

相关问题 更多 >