Check if any tests raise a deprecation warning with pytest

杀马特。学长 韩版系。学妹 提交于 2019-12-23 09:15:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!