我有一个单元测试来检查构造函数中的异常:
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)