How To Ignore Warnings In Pytest (With Examples)
You know that annoying feeling when trying to run your code and unrelated warnings pop up?
Like a DeprecationWarning
from an external library that you have no control over.
While useful, warnings can clutter your console output and obscure important results.
How do you silence warnings in Pytest? Should you disable ALL warnings? What about important ones?
Warnings are vital signals that something in your code might not be right but not necessarily an error — such as deprecated functions or potential runtime issues.
They help you anticipate and correct issues before they escalate into more significant problems.
In this article, you’ll learn to handle warnings elegantly in Pytest — using different techniques.
First, you’ll explore the warnings
module, what it means, and the different types of warnings.
Then you’ll learn how Pytest manages warnings by default and how to override the configuration to suit your needs, along with some best practices to manage warnings.
Let’s begin.
Understanding Python Warnings
Before we dive into code, let’s understand what the warnings
module in Python is and why it was designed in the first place.
The warnings
module is an extension of the base Exception
class to highlight issues that are worth knowing about but not as critical to breaking the program as an exception
.
It can notify you about syntax or functionality that still works but may not be supported in future releases.
The official docs explain the different types of warnings
DeprecationWarning
— Warns about obsolete features that are expected to be removed in future versions.UserWarning
— Base class for warnings generated by user code.SyntaxWarning
— Warns about dubious syntax.RuntimeWarning
— Warns about dubious runtime behavior.FutureWarning
— Similar toDeprecationWarning
, warns about deprecated features whose discontinuation is planned in the distant future.
You can easily generate warnings using the warnings
library for example,1
warnings.warn(message, category=None, stacklevel=1)
message
: The warning message, typically a string.category
: Category of the warning (e.g.,DeprecationWarning
,UserWarning
), which helps in filtering as discussed previously.stacklevel
: An integer that indicates the stack frame depth to use when the origin of the warning is reported, helping you identify where the warning was triggered from. Astacklevel
of 1 points to the caller of thewarn()
function.
When using external libraries, very often you may come across DeprecationWarning
or SyntaxWarning
.
Now that you have some background on warnings, let’s move on to why you came here in the first place — how to ignore warnings in Pytest.
After all, warnings raised by external libraries are not your problem and can be abstracted.
Disabling Warnings Entirely In Pytest
You can use the --disable-warnings
command-line option to suppress the warning summary entirely from the test run output.1
pytest --disable-warnings
This is an extreme option and comes with its drawbacks, the biggest is that you won’t know when there’s a warning that concerns you and should be addressed (as Pytest will filter out ALL warnings).
You can also disable warnings capture using the -p no:warnings
command-line option.1
pytest -p no:warnings
Or using a config file and Pytest addoption1
2[pytest]
addopts = -p no:warnings
A better option is to suppress or filter specific warnings using the pytest.ini
file.
If you’re not familiar with using Pytest config files, I highly recommend you check out this article to learn more.
Suppress or Filter Warnings
Pytest allows you to suppress or filter warnings easily using the pytest.ini
file and filterwarnings
option.
This is a much better option that allows you to filter out specific warnings, as opposed to all warnings.1
2
3[pytest]
filterwarnings =
ignore::DeprecationWarning
This setting will ignore any DeprecationWarnings
in your test modules.
The configuration below will ignore all user warnings and specific deprecation warnings matching a regex, but will transform all other warnings into errors.1
2
3
4
5[pytest]
filterwarnings =
error
ignore::UserWarning
ignore:function ham\(\) is deprecated:DeprecationWarning
Suppress or Filter (Specific) Library Warnings
A cleaner approach is to ignore a specific warning from the library that’s raising it, this can be done by1
2
3[pytest]
filterwarnings =
ignore::DeprecationWarning:module_name.*
For example1
2
3
4[pytest]
filterwarnings =
ignore::DeprecationWarning:botocore.*:
ignore::FutureWarning:pandas.*
This will suppress all warnings from that library.
Example Code
As we mostly do on this website, let’s write some example code to illustrate the above.
Clone the repo and set up your virtual environment.
examples/my_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14import warnings
def old_function():
warnings.warn(
"old_function() is deprecated; use new_function() instead.",
DeprecationWarning,
stacklevel=2,
)
def new_function():
print("This is the new function.")
old_function()
Running this we get the warning.1
python examples/my_module.py
Let’s write a test.
tests/test_warnings.py
1
2
3
4
5import warnings
from examples.my_module import old_function
def test_function():
old_function()
Running the test1
pytest
Example Test — Suppress Warnings
Let’s include a pytest.ini
file with the following content1
2
3[pytest]
filterwarnings =
ignore::DeprecationWarning
Running this,
Smooth.
Let’s add a SyntaxWarning
to our code.
examples/my_module.py
1
2
3
4
5
6
7
8
9
10
11def old_function():
warnings.warn(
"old_function() is deprecated; use new_function() instead.",
DeprecationWarning,
stacklevel=2,
)
warnings.warn(
"The syntax will be removed in version 2.0.0.",
SyntaxWarning,
stacklevel=2,
)
Let’s run the Pytest
As you can see, Pytest suppressed the DeprecationWarning
but still shows the SyntaxWarning
.
Modifying our pytest.ini
file1
2
3
4[pytest]
filterwarnings =
ignore::DeprecationWarning
ignore::SyntaxWarning
We now have a clean output.
You can play around a lot more with warnings and their suppression.
Using Markers To Filter Warnings
Pytest also gives you the option to use markers to add warning filters to specific tests and test modules which gives greater control.1
2
3
4
5
6
7
8
9
10
11
12import warnings
import pytest
def api_v1():
# Generates a warning with a specific message.
warnings.warn("api v1, should use functions from v2", UserWarning)
return 1
def test_one():
# Will ignore warnings with the message containing "api v1".
assert api_v1() == 1
Ensuring Code Triggers Deprecation Warning
Now if you’re on the generator side, i.e. you want to test that your code triggers a DeprecationWarning
, you can also use Pytest to test it.
You can use the pytest.deprecated_call() method.1
2
3def test_myfunction_deprecated():
with pytest.deprecated_call():
old_function()
This test will fail if old_function
does not issue a deprecation warning.
Conclusion
We’re here at the end.
I hope you found what you were looking for — how to easily filter warnings in Pytest - whether from your own libraries, external packages, or even Pytest itself.
In this article, we covered some basics about Python warnings, what they are, and how to use them.
Then we looked at the most important bit, how to disable or suppress warnings in Pytest (especially from external libraries).
Lastly, you also learned how to suppress warnings for a specific library, using Pytest markers to filter warnings and even assert your own DeprecationWarnings.
If you have any ideas for improvement or like me to cover any topics please comment below or send me a message via Twitter, GitHub or Email.
Additional Reading
Example Code Used In This Article
How to capture warnings - Official Pytest Docs
What Is pytest.ini
And How To Save Time Using Pytest Config
How To Use Pytest With Command Line Options (Easy To Follow Guide)
Pytest Config Files - A Practical Guide To Good Config Management
Ultimate Guide To Pytest Markers And Good Test Management