gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:43:01 24 4
gpt4 key购买 nike

我使用 openpyxl 来解析 .xlsm 文件,并使用 pytest 进行测试。

当我打开文件时,我得到:
OpenPyxl -> UserWarning:不支持数据验证扩展,将被删除

这并不是真正的问题。程序可以运行,但我无法更改 .xlsm 文件来修复该问题。

但是...当我使用以下命令运行 pytest 时:

def test_wrong_file_format():  
assert check_excel(open_excel('file.xlsm')) == True

我会收到我提到的警告,尽管 check_excel(open_excel('file.xlsm')) 返回 True 并且测试应该成功...

是否有一种很好的方法来告诉 pytest“这不是一个错误,而是一个功能”,并且即使收到此警告,测试也应该通过?

除了使用类似的东西之外还有其他方法吗:

with pytest.warns(UserWarning):
warnings.warn("my warning", UserWarning)

谢谢,
汤姆

最佳答案

根据官方文档( pytest ),@pytest.mark.filterwarnings 是正确的方法。只需选择正确的参数,例如:

@pytest.mark.filterwarnings('ignore::UserWarning')

关于python - 如何在pytest中忽略python UserWarning?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53434877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com