Skip to content Skip to sidebar Skip to footer

In Pytest, How To Assert If An Exception (which Is Inherited From A Parent Exception Class) Is Raised?

What I have done : I have a function def get_holidays(): which raises a Timeout error. My test function test_get_holidays_raises_ioerror(): first sets requests.get.side_effect = I

Solution 1:

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

Post a Comment for "In Pytest, How To Assert If An Exception (which Is Inherited From A Parent Exception Class) Is Raised?"