Explain Codes LogoExplain Codes Logo

How to suppress py.test internal deprecation warnings

python
pytest
warning-suppression
testing
Alex KataevbyAlex Kataev·Jan 9, 2025
TLDR

There's a quick fix to quell py.test deprecation warnings: use the pytest -W ignore::DeprecationWarning command or drop ignore::DeprecationWarning into the filterwarnings slot in a pytest.ini file.

Here's how it translates in command-line:

pytest -W ignore::DeprecationWarning

And with a sprinkle of pytest.ini magic:

[pytest]
filterwarnings = ignore::DeprecationWarning

But suppressing warnings isn't just about one-liners. Let's explore how you can firmly control Python's warning system, keeping your test output as clean as a fresh install.

Expand your command-line power

When you're sick of all the console chatter, you have plenty of scripts in your silencing toolkit:

-W option

This -W command lets you stealthily add warning filters just like in pytest.ini, but straight from the command-line:

# Just like Hogwarts, but for warning filters!
pytest -W ignore::UserWarning -W ignore::RuntimeWarning

Total pytest silence

For an ascetic, noise-free pytest output:

pytest --disable-pytest-warnings

Hush specific modules

Got some blabbermouth modules? Target them directly:

# "Hush, you noisy module!"
pytest -W ignore::DeprecationWarning:noise_maker.*

Fine-tuning in pytest.ini

And then sometimes you need fine control with some regex mastery in pytest.ini:

[pytest]
filterwarnings =
    ignore::DeprecationWarning:noise_maker.*:
    default::UserWarning:another_module.*

Keep the pytest.ini filters up to date as your code evolves and new deprecations arise.

Use -p no:warnings

Adding -p no:warnings in pytest.ini or as an addopts argument is a neat trick to also hide all warnings:

[pytest]
addopts = -p no:warnings

Keep an eye on important warnings

Make sure you're not muting high-value deprecation warnings hinting at future breaking changes in dependencies. Periodically review your suppressed warnings — it's like doing maintenance work!

Bonus tips — real-life examples and more

Now let's get hands-on and explore some life-saving tricks revolving around warning suppression:

Test-run scenarios: Control warnings, control the world*

Got CI/CD pipelines? You might want to suppress warnings on CI runs while keeping them visible in local dev. Use environment variables in your pytest.ini:

[pytest] filterwarnings = ignore::DeprecationWarning if 'CI' in os.environ

*Alright, perhaps not the world, but close!

Silence erupting third-party geysers

Some third-party vendors have noisy libraries. Silence only their warnings without affecting others:

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

Debugging? Unmask the warning monsters

You might need to temporarily see your masked warning monsters when debugging. Just comment out or remove the filters!

Delegate logging to record warnings

Use the logging functionality to track warnings in a separate log file without muddying the terminal:

import logging import warnings logging.captureWarnings(True) warnings.filterwarnings('default', category=DeprecationWarning, module='myapp') logger = logging.getLogger('py.warnings') handler = logging.FileHandler('warnings.log') logger.addHandler(handler)