Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress pytest warnings for other peoples code only

Tags:

pytest

I'm trying out pytest for the first time. How do I suppress warnings issued about other peoples code that my code depends on without suppressing warnings about my own code?

Right now I have this in my pytest.ini so I don't have to see pytest warn me about some deprecation on the jsonschema package that I'm using.

[pytest]
filterwarnings =
    ignore::DeprecationWarning

But now if I write anything in my own code that should fire of a deprecation warning I'll miss it.

like image 847
oivvio Avatar asked Feb 16 '19 11:02

oivvio


People also ask

How do I hide Pytest warnings?

Disabling warnings summary Although not recommended, you can use the --disable-warnings command-line option to suppress the warning summary entirely from the test run output.

How do I ignore a warning in Python?

All warnings are ignored by setting the first parameter of warnings. simplefilter() , action , to 'ignore' . See the official documentation for the warning filters you can set for action .

How do I check my Pytest warnings?

-r chars show extra test summary info as specified by chars (f)ailed, (E)error, (s)skipped, (x)failed, (X)passed (w)pytest-warnings (a)all. This will allow to show warnings in the report (top portion of the record) will list which pytest plugins use deprecated arguments (in my case bellow):

What is DeprecationWarning?

DeprecationWarning errors are logged by the Node. js runtime when your code (or one of the dependencies in your code) calls a deprecated API. These warnings usually include a DEP deprecation code. They are logged using console.


1 Answers

The syntax for pytest-warning is action:message:category:module:lineno. You can use this config for ignoring only jsonschema:

[pytest]
filterwarnings =
    ignore::DeprecationWarning:jsonschema

You can also use regex in those fields. If you want to exclude all warnings except yours:

[pytest]
filterwarnings =
    ignore::DeprecationWarning:!yourtestmodule

Pytest uses the same filterwarning as python. You can learn more about python warnings here: https://docs.python.org/3/library/warnings.html#warning-filter

Source: https://github.com/fschulze/pytest-warnings/blob/master/pytest_warnings/init.py#L18

like image 73
SilentGuy Avatar answered Oct 21 '22 18:10

SilentGuy