pytest - Suppress DeprecationWarning from specific 3rd party modules

Viewed 5740

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

3 Answers

The answer from @Santiago MagariƱos does not work for me (but it put me on the correct path to find solution - so many thanks).

I use Python 3.9, pytest 6.2.1 and trying to suppress warnings from selenium 3.141.0.

I have realized that I need to prepend .* before the module name:

[pytest]
filterwarnings = ignore:::.*.selenium

or use the full "path". So to suppress the warning

../../../../../../.local/share/virtualenvs/common-bjARi2zp/lib/python3.9/site-packages/selenium/webdriver/support/wait.py:28
  /home/vaclav/.local/share/virtualenvs/common-bjARi2zp/lib/python3.9/site-packages/selenium/webdriver/support/wait.py:28: DeprecationWarning: invalid escape sequence \ 
    """Constructor, takes a WebDriver instance and timeout in seconds.

I need to use this filter in pytest.ini file:

[pytest]
filterwarnings = ignore:::.home.vaclav..local.share.virtualenvs.common-bjARi2zp.lib.python3.9.site-packages.selenium

if you are using pyproject.toml file for pytest configuration you can use:

 [tool.pytest.ini_options]
 testpaths = ["./tests/unit"]
 filterwarnings = ["ignore:::.*third_party_package.module:123", "ignore:::.*another_module*"]

ignore:::.*third_party_package.module:123 ignores in specific warning at specific line ignore:::.*another_module* ignores all warnings in the module.

Notice that you can have multiple ignores. You need to list them in []. Also, you can't have *third_party_package/module:123 you must replace all / with .

Related