检查是否有任何测试使用 pytest 引发弃用警告 [英] Check if any tests raise a deprecation warning with pytest

查看:39
本文介绍了检查是否有任何测试使用 pytest 引发弃用警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pytest 在 Python 包中运行测试,我想知道作为测试的一部分执行的任何代码是否引发弃用警告(当所有测试都通过时).有人知道这样做的方法吗?

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?

推荐答案

代码

import warnings
warnings.simplefilter("error")

会将(所有)警告变成错误,这可能会有所帮助.

will turn (all) warnings into errors, which may help.

或者,您可以使用

import warnings

with warnings.catch_warnings(record=True) as w:
    warnings.warn("deprecated", DeprecationWarning)
    print(w)

#>>> [<warnings.WarningMessage object at 0x7fee80484f50>]

然后在该列表上断言.

这篇关于检查是否有任何测试使用 pytest 引发弃用警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆