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

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