且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在pytest中,如何断言是否引发了异常(该异常是从父异常类继承的)?

更新时间:2022-02-17 03:32:44

pytest捕获ExceptionInfo对象中的异常.您可以在例外之后比较确切的类型.

pytest captures the exception in an ExceptionInfo object. You can compare the exact type after the exception.

def test_get_holidays_raises_ioerror():
    requests.get.side_effect = IOError
    with pytest.raises(IOError) as excinfo:
        get_holidays()
    assert type(excinfo.value) is IOError