问题
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?
回答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.
回答2:
Starting with pytest 3.1, warnings are automatically displayed at the end of the session: see https://docs.pytest.org/en/latest/warnings.html
来源:https://stackoverflow.com/questions/18920576/check-if-any-tests-raise-a-deprecation-warning-with-pytest