pytest异常处理
raises: 在断言一些代码块或者函数时会引发意料之中的异常或者其他失败的异常,导致程序无法运行时,使用 raises 捕获匹配到的异常,可以继续让代码正常运行。
可以打印错误类型(print(e.type)),错误信息(print(e.value.args[0])
def test_04(self):
with pytest.raises(Exception) as e:
assert 1==2/0
print("打印异常",e,type(e))
print(e.type)
print(e.value.args[0])
if "Exception" in str(e):
assert 1==1
def test_05(self):
assert 1==2/0
print("不打印异常处理")
raise 的异常应该是当前代码块最后一行,如果在其后面还有代码,那么将不会被执行
def test_04(self):
with pytest.raises(Exception) as e:
assert 1==2/0
print("不被执行打印异常",e,type(e)) #这行不被执行
print("被执行打印异常",e,type(e)) #这行被执行
上班求生存,下班求发展

浙公网安备 33010602011771号