gpt4 book ai didi

python - pytest - 抑制来自特定 3rd 方模块的 DeprecationWarning

转载 作者:行者123 更新时间:2023-12-04 16:44:13 27 4
gpt4 key购买 nike

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

This answer帮助我中途到达那里。如果我像这样运行 pytest:$ pytest ./tests/我得到:

$ 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我得到:
============================= 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 ===============================

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

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

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

最佳答案

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

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

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

“个别警告过滤器被指定为由冒号分隔的字段序列:”
action:message:category:module:line

关于python - pytest - 抑制来自特定 3rd 方模块的 DeprecationWarning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58399870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com