How to get rid of third-party library warnings while running unit tests?

SkyWalker :

I setup my project using PyScaffold and while running unit tests using pytest I get the following third party warning that I'd like to get rid of but don't know how:

==================================== warnings summary ====================================
c:\dev\pyrepo\lib\site-packages\patsy\constraint.py:13
  c:\dev\pyrepo\lib\site-packages\patsy\constraint.py:13: DeprecationWarning: Using or importing
 the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in
 3.9 it will stop working
    from collections import Mapping

-- Docs: https://docs.pytest.org/en/latest/warnings.html

What's the best way to avoid warnings from third-party libraries like this but not my own project code warnings?

hurlenko :

There are multiple ways to suppress warnings:

  • using command-line arguments

To hide the warning completely use

pytest . -W ignore::DeprecationWarning

This command will hide warnings summary but will show 1 passed, 1 warning message

pytest . --disable-warnings
  • creating pytest.ini with the following content
[pytest]
filterwarnings =
    ignore::DeprecationWarning

You can also use regex patterns:

ignore:.*U.*mode is deprecated:DeprecationWarning

From the docs:

This will ignore all warnings of type DeprecationWarning where the start of the message matches the regular expression .*U.*mode is deprecated.

  • marking your test_ function with @pytest.mark.filterwarnings("ignore::DeprecationWarning")

  • using PYTHONWARNINGS environment variable

PYTHONWARNINGS="ignore::DeprecationWarning" pytest .

It has the same syntax as the -W command-line arg. More here.

More details can be found in the pytest docs

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=361296&siteId=1