python pytest raises()

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章