I am using pytest to run tests in a Python package, and I would like to know if any of the code that is executed as part of the tests is raising deprecation warnings (when all tests are passing). Does anyone know of a way to do this?
Asked
Active
Viewed 2,203 times
2 Answers
1
The code
import warnings
warnings.simplefilter("error")
will turn (all) warnings into errors, which may help.
Alternatively, you can get a list of generated warnings with
import warnings
with warnings.catch_warnings(record=True) as w:
warnings.warn("deprecated", DeprecationWarning)
print(w)
#>>> [<warnings.WarningMessage object at 0x7fee80484f50>]
and then assert on that list.

Veedrac
- 58,273
- 15
- 112
- 169
-
Please note that this example is no longer valid as python2.7 since the DeprecationWarnings are filtered out by default – Romuald Brunet Jan 09 '17 at 09:42
1
Starting with pytest 3.1, warnings are automatically displayed at the end of the session: see https://docs.pytest.org/en/latest/warnings.html

Wolfgang Ulmer
- 3,450
- 2
- 21
- 13