Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在运行单元测试时消除第三方库警告?_Python_Unit Testing_Pytest - Fatal编程技术网

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

Python 如何在运行单元测试时消除第三方库警告?,python,unit-testing,pytest,Python,Unit Testing,Pytest,我使用PyScaffold设置了我的项目,在使用pytest运行单元测试时,我得到了以下第三方警告,我想摆脱,但不知道如何摆脱: ==================================== warnings summary ==================================== c:\dev\pyrepo\lib\site-packages\patsy\constraint.py:13 c:\dev\pyrepo\lib\site-packages\patsy

我使用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

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

抑制警告的方法有多种:

  • 使用命令行参数
要完全隐藏警告,请使用

pytest . -W ignore::DeprecationWarning
此命令将隐藏
警告摘要
,但将显示
1已通过,1条警告
消息

pytest . --disable-warnings
  • 使用以下内容创建
    pytest.ini
您还可以使用正则表达式模式:

ignore:.*U.*mode is deprecated:DeprecationWarning
从文档中:

这将忽略所有类型为DeprecationWarning的警告,其中消息的开头与正则表达式
*U.*模式为deprecated

  • 使用
    @pytest.mark.filterwarnings(“ignore::DeprecationWarning”)

  • 使用
    pythonwarning
    环境变量

它的语法与
-W
命令行参数相同。更多

有关更多详细信息,请参阅

ignore:.*U.*mode is deprecated:DeprecationWarning
PYTHONWARNINGS="ignore::DeprecationWarning" pytest .