6

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?

astrofrog
  • 32,883
  • 32
  • 90
  • 131

2 Answers2

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
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