pytest - 抑制来自特定 3rd 方模块的 DeprecationWarning [英] pytest - Suppress DeprecationWarning from specific 3rd party modules

查看:21
本文介绍了pytest - 抑制来自特定 3rd 方模块的 DeprecationWarning的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行 pytest 时,我收到了来自 3rd 方库的一些弃用警告.我想知道我自己的代码中的任何弃用警告,但不是在与另一个 3rd 方库捆绑在一起的库的供应商副本中.

When I run pytest I'm getting some deprecation warnings from a 3rd party library. I'd like to be informed about any deprecation warnings in my own code, but not in a vendored copy of a library bundled with another 3rd-party library.

这个答案帮助我完成了一半的工作.如果我像这样运行 pytest:$ pytest ./tests/ 我得到:

This answer was helpful in getting me partway there. If I run pytest like this: $ pytest ./tests/ I get:

$ pytest ./tests/
============================= test session starts ==============================
platform linux -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: /home/whlt/repos/tj-image-resizer/tests, inifile: pytest.ini
collected 5 items                                                              

tests/test_file1.py .                                                   [ 20%]
tests/test_file2.py ....                                                [100%]

=============================== warnings summary ===============================
/home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1
/home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1
  /home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
    from collections import Mapping, MutableMapping

-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================== 5 passed, 2 warnings in 2.54s =========================

但是如果我像这样运行 pytest:$ pytest ./tests/-W ignore::DeprecationWarning 我得到:

but if I run pytest like this: $ pytest ./tests/ -W ignore::DeprecationWarning I get:

============================= test session starts ==============================
platform linux -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: /home/whlt/repos/tj-image-resizer/tests, inifile: pytest.ini
collected 5 items                                                              

tests/test_file1.py .                                                   [ 20%]
tests/test_file2.py ....                                                [100%]

============================== 5 passed in 2.61s ===============================

第二个输出显示过滤器有效,但这也会隐藏我希望看到的由我自己的代码产生的任何弃用警告.

This second output shows me that the filter works, but that will also hide any deprecation warnings I'd like to seeing resulting from my own code.

这个问题的一部分是我不确定在忽略过滤器中尝试引用哪个模块.我试过 $ pytest ./tests/-W ignore::DeprecationWarning:urllib3.*: 并且我试过 $ pytest ./tests/-W ignore::DeprecationWarning:botocore.*:.这两个结果与第一个没有过滤的示例的输出相同.

Part of this issue is that I'm not sure which module to try referencing in the ignore filter. I've tried $ pytest ./tests/ -W ignore::DeprecationWarning:urllib3.*: and I've tried $ pytest ./tests/ -W ignore::DeprecationWarning:botocore.*:. Both of these result in the same output as the first example with no filtering.

如何从 urllib3 的版本中过滤掉 DeprecationWarnings,该版本与 botocore 包含的 requests 的供应商版本打包在一起(当我使用 boto3 库运行命令)?

How can I filter out DeprecationWarnings from the version of urllib3 packaged with the vendored version of requests included with botocore (which gets called when I run commands with the boto3 library)?

推荐答案

您应该使用警告过滤器选项(ini 或标记):

You should use the warning filters options (ini or marks):

[pytest]
filterwarnings =
    ignore::DeprecationWarning:botocore.*:

来源:https://docs.python.org/3/library/warnings.html#default-warning-filter

个别警告过滤器被指定为以冒号分隔的字段序列:"

"Individual warnings filters are specified as a sequence of fields separated by colons:"

action:message:category:module:line

这篇关于pytest - 抑制来自特定 3rd 方模块的 DeprecationWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆