栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何正确断言pytest中引发了异常?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何正确断言pytest中引发了异常?

pytest.raises(Exception)

是您所需要的。

import pytestdef test_passes():    with pytest.raises(Exception) as e_info:        x = 1 / 0def test_passes_without_info():    with pytest.raises(Exception):        x = 1 / 0def test_fails():    with pytest.raises(Exception) as e_info:        x = 1 / 1def test_fails_without_info():    with pytest.raises(Exception):        x = 1 / 1# Don't do this. Assertions are caught as exceptions.def test_passes_but_should_not():    try:        x = 1 / 1        assert False    except Exception:        assert True# Even if the appropriate exception is caught, it is bad style,# because the test result is less informative# than it would be with pytest.raises(e)# (it just says pass or fail.)def test_passes_but_bad_style():    try:        x = 1 / 0        assert False    except ZeroDivisionError:        assert Truedef test_fails_but_bad_style():    try:        x = 1 / 1        assert False    except ZeroDivisionError:        assert True

输出量

============================================================================================= test session starts ==============================================================================================platform linux2 -- Python 2.7.6 -- py-1.4.26 -- pytest-2.6.4collected 7 itemstest.py ..FF..F=================================================================================================== FAILURES ===================================================================================================__________________________________________________________________________________________________ test_fails __________________________________________________________________________________________________    def test_fails():        with pytest.raises(Exception) as e_info:>x = 1 / 1EFailed: DID NOT RAISEtest.py:13: Failed___________________________________________________________________________________________ test_fails_without_info ____________________________________________________________________________________________    def test_fails_without_info():        with pytest.raises(Exception):>x = 1 / 1EFailed: DID NOT RAISEtest.py:17: Failed___________________________________________________________________________________________ test_fails_but_bad_style ___________________________________________________________________________________________    def test_fails_but_bad_style():        try: x = 1 / 1>assert FalseEassert Falsetest.py:43: AssertionError====================================================================================== 3 failed, 4 passed in 0.02 seconds ======================================================================================

请注意,

e_info
将保存异常对象,以便您可以从中提取详细信息。例如,如果要检查异常调用堆栈或内部的另一个嵌套异常。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/611944.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号