简体   繁体   English

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

[英]How to get rid of third-party library warnings while running unit tests?

I setup my project using PyScaffold and while running unit tests using pytest I get the following third party warning that I'd like to get rid of but don't know how:我使用 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

What's the best way to avoid warnings from third-party libraries like this but not my own project code warnings?避免来自第三方库这样的警告而不是我自己的项目代码警告的最佳方法是什么?

There are multiple ways to suppress warnings:有多种方法可以抑制警告:

  • using command-line arguments使用命令行参数

To hide the warning completely use要完全隐藏警告,请使用

pytest . -W ignore::DeprecationWarning

This command will hide warnings summary but will show 1 passed, 1 warning message此命令将隐藏warnings summary但会显示1 passed, 1 warning消息

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

You can also use regex patterns:您还可以使用正则表达式模式:

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

From the docs:从文档:

This will ignore all warnings of type DeprecationWarning where the start of the message matches the regular expression .*U.*mode is deprecated .这将忽略所有类型为 DeprecationWarning 的警告,其中消息的开头与正则表达式.*U.*mode is deprecated匹配。

  • marking your test_ function with @pytest.mark.filterwarnings("ignore::DeprecationWarning")使用@pytest.mark.filterwarnings("ignore::DeprecationWarning")标记您的test_函数

  • using PYTHONWARNINGS environment variable使用PYTHONWARNINGS环境变量

PYTHONWARNINGS="ignore::DeprecationWarning" pytest .

It has the same syntax as the -W command-line arg.它的语法与-W命令行参数相同。 More here .更多在这里

More details can be found in the pytest docs可以在pytest 文档中找到更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM