pytest - 抑制特定第三方模块的 DeprecationWarning
pytest - 抑制特定第三方模块的 DeprecationWarning
当我运行pytest时,我从第三方库得到了一些弃用警告。我想要得到有关自己代码中的任何弃用警告的通知,但不想得到与另一个第三方库捆绑在一起的库的版本的弃用警告。
这个答案在一定程度上帮助了我。如果我像这样运行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.*:。这两种方法的输出与第一个示例相同,没有进行过滤。
我该如何过滤掉由运行带有boto3库的命令时调用的botocore附带的requests的vendored版本中的urllib3引起的DeprecationWarnings?