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

标签 python testing pytest

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

最佳答案

代码

import warnings
warnings.simplefilter("error")

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

或者,您可以获取生成的警告列表

import warnings

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

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

然后对该列表进行断言。

关于python - 使用 pytest 检查是否有任何测试引发弃用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18920576/

相关文章:

python - 向量化 numpy 运算

python - pandas 根据匹配记录的有序列表的 TOP 1 结果连接数据帧

javascript - 使用服务器端 Javascript 检查 Web 服务器上的文件是否存在

sockets - jmeter - 异步 TCP 采样器选项?

python - pycharm pytestrunner PluginManager 意外的关键字参数

python - 如何在pytest中忽略python UserWarning?

python - 循环脚本以生成多个图像

python - 尝试使用 gdata 2.0.16 python 库检索用户时出现 "Insecure HTTP requests not permitted. Use HTTPS."

java - mockito 和 jetty-client : receiving NullPointerException when start() and stop() methods are invoked

python - 使用 capsys 捕获 stdout 和 stderr