pytest - Suppress DeprecationWarning from specific 3rd party modules

安稳与你 提交于 2019-12-24 05:22:38

问题


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.

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 =========================

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.

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.

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)?


回答1:


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

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

Source: 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


来源:https://stackoverflow.com/questions/58399870/pytest-suppress-deprecationwarning-from-specific-3rd-party-modules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!