【问题标题】:How can I hide my stack frames in a TestCase subclass?如何在 TestCase 子类中隐藏我的堆栈帧?
【发布时间】:2012-09-25 12:31:42
【问题描述】:

我想向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)

请注意,自定义断言方法会导致堆栈跟踪有两帧,一帧在方法本身内部,而普通的unittest 方法只有一帧,即用户代码中的相关行。如何将这种框架隐藏行为应用到我自己的方法中?

【问题讨论】:

    标签: python unit-testing testing subclass stack-trace


    【解决方案1】:

    此问题已回答by Peter Otten on 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)
    $ 
    

    【讨论】:

    • 我不敢相信这个问题/答案没有得到更多的投票!很棒的信息以及为什么我
    • 这个解决方案太有用了!
    • 不确定 python.org 上的 pipermail 是否更改,但彼得解决方案的正确链接是 mail.python.org/pipermail/python-list/2012-October/632386.html 。除此之外,非常感谢您的指点,真的帮助了我。
    猜你喜欢
    • 2014-06-30
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 2012-12-07
    • 2018-08-12
    • 1970-01-01
    相关资源
    最近更新 更多