コンストラクターで例外をチェックする単体テストがあります。
import unittest
from jaboci import Jacobi
class TestJacobi(unittest.TestCase):
def test_even(self):
a = 11
n = 12
Jacobi(a, n)
self.assertRaises(ValueError, Jacobi, a, n)
if __name__ == '__main__':
unittest.main()
テスト中のクラス:
class Jacobi:
def __init__(self, a, n):
self.a = a
self.n = n
if n % 2 == 0:
raise ValueError("N must be odd.")
で unittest を実行すると-m unittest discover
、テストが失敗します。
E
======================================================================
ERROR: test_even (test_jacobi.TestJacobi)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/prasopes/prg/python/PycharmProjects/jacobi_symbol/test_jacobi.py", line 9, in test_even
Jacobi(a, n)
File "/home/prasopes/prg/python/PycharmProjects/jacobi_symbol/jaboci.py", line 7, in __init__
raise ValueError("N must be odd.")
ValueError: N must be odd.
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)