检查是否有任何测试使用pytest引发了弃用警告

ast*_*rog 6 python testing pytest

我使用pytest运行在一个Python包的考验,我想知道是否有被作为测试是提高废弃警告(当所有的测试都通过)的一部分执行的代码.有谁知道这样做的方法?

Vee*_*rac 1

代码

import warnings
warnings.simplefilter("error")
Run Code Online (Sandbox Code Playgroud)

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

或者,您可以使用以下命令获取生成的警告列表

import warnings

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

#>>> [<warnings.WarningMessage object at 0x7fee80484f50>]
Run Code Online (Sandbox Code Playgroud)

然后在该列表上断言。