import pytest
def test_zero_division():
with pytest.raises(ZeroDivisionError) as excinfo://在该上下文下写跑一场的函数
1 / 0
assert excinfo.type == ZeroDivisionError # 断言异常类型type
assert "division by zero" in str(excinfo.value) # 断言异常value值
参考:
https://learning-pytest.readthedocs.io/zh/latest/doc/test-function/exception.html
https://www.cnblogs.com/yoyoketang/p/9469996.html