pytestを使って、例外やエラーの発生を確認する方法を書いていきます。
コード例
with pytest.raises を使って確認することができます。
import pytest import tutil as tutil def test_raise_error(): # ValueErrorが発生することを確認 with pytest.raises(ValueError): tutil.raise_error()
テスト対象のコード(tutil.py)は以下の通りです。
def raise_error(): raise ValueError("テスト目的のエラー")