pytest で例外を処理する2021年01月21日 13時28分54秒

python のテストフレームワーク、pytest には例外を処理する為の機構がある。例外が投げられるのをテストするのに必要だ。

pytest.raises は with 句で使う。

def test_exception():
    with pytest.raises( Exception ) as ex:   
        raise Exception( 'As expected' )
    assert execinfo.value.args[0] == 'As expected'