9

我想向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方法只有一帧,即用户代码中的相关行。如何将此框架隐藏行为应用于我自己的方法?

4

1 回答 1

16

Peter Otten 在 comp.lang.python 上回答了这个问题。

将 MyTestCase 移动到一个单独的模块中并定义一个全局变量__unittest = True

$ cat mytestcase.py 
import unittest

__unittest = True

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)

$ cat mytestcase_demo.py 
import unittest
from mytestcase import MyTestCase

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)

if __name__ == "__main__":
    unittest.main()

$ python mytestcase_demo.py 
FF
======================================================================
FAIL: test_custom_assertion (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "mytestcase_demo.py", line 11, in test_custom_assertion
    self.assertLengthIsOne(self.seq)
AssertionError: length is not one

======================================================================
FAIL: test_stock_unittest_assertion (__main__.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "mytestcase_demo.py", line 8, in test_stock_unittest_assertion
    self.assertEqual(len(self.seq), 1)
AssertionError: 5 != 1

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=2)
$ 
于 2012-10-25T13:17:44.967 回答