类中有一个assertRaises()
for 方面TestCase
,您希望确保待测试代码引发错误。
如果您想在此时引发错误并中止对该单元的测试(并继续下一个单元测试),只需引发未捕获的异常;单元测试模块将捕获它:
raise NotImplementedError("malformed sub: %r" % (sub,))
我认为除了直接引发错误以说明单元测试用例导致错误之外,没有任何其他 API 方面可用。
class PassingTest(unittest.TestCase):
def runTest(self):
self.assertTrue(True)
class FailingTest(unittest.TestCase):
def runTest(self):
self.assertTrue(False)
class ErrorTest(unittest.TestCase):
def runTest(self):
raise NotImplementedError("error")
class PassingTest2(unittest.TestCase):
def runTest(self):
self.assertTrue(True)
结果是:
EF..
======================================================================
ERROR: runTest (__main__.ErrorTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./t.py", line 15, in runTest
raise NotImplementedError("error")
NotImplementedError: error
======================================================================
FAIL: runTest (__main__.FailingTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "./t.py", line 11, in runTest
self.assertTrue(False)
AssertionError: False is not true
----------------------------------------------------------------------
Ran 4 tests in 0.002s
FAILED (failures=1, errors=1)