raises: 在断言一些代码块或者函数时会引发意料之中的异常或者其他失败的异常,导致程序无法运行时,使用 raises 捕获匹配到的异常,可以继续让代码正常运行。
import pytest
def exc(x):
if x == 0:
raise ValueError("value not 0 or None")
return 2 / x
def test_raises():
with pytest.raises(ValueError, match="value not 0 or None"):
exc(0)
assert eval("1 + 2") == 3
原文:https://www.cnblogs.com/lhTest/p/14777649.html