我想向TestCase
子类添加自定义断言方法。我试图从unittest
模块中复制我的实现,以便它TestCase
尽可能地匹配常规的行为。(我宁愿只委托给self.assertEqual()
,但这会导致更多的回溯噪音,见下文。)unittest
当报告失败的断言时,该模块似乎会自动隐藏其实现的一些内部细节。
import unittest
class MyTestCase(unittest.TestCase):
def assertLengthIsOne(self, sequence, msg=None):
if len(sequence) != 1:
msg = self._formatMessage(msg, "length is not one")
raise self.failureException(msg)
class TestFoo(MyTestCase):
seq = (1, 2, 3, 4, 5)
def test_stock_unittest_assertion(self):
self.assertEqual(len(self.seq), 1)
def test_custom_assertion(self):
self.assertLengthIsOne(self.seq)
unittest.main()
它的输出是这样的:
amoe@vuurvlieg $ python unittest-demo.py
FF
======================================================================
FAIL: test_custom_assertion (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
File "unittest-demo.py", line 16, in test_custom_assertion
self.assertLengthIsOne(self.seq)
File "unittest-demo.py", line 7, in assertLengthIsOne
raise self.failureException(msg)
AssertionError: length is not one
======================================================================
FAIL: test_stock_unittest_assertion (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
File "unittest-demo.py", line 13, in test_stock_unittest_assertion
self.assertEqual(len(self.seq), 1)
AssertionError: 5 != 1
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (failures=2)
请注意,自定义 assert 方法会导致堆栈跟踪有两帧,一帧在方法本身内部,而 stockunittest
方法只有一帧,即用户代码中的相关行。如何将此框架隐藏行为应用于我自己的方法?