【问题标题】:Make Python unittest show AssertionError but no Traceback使 Python unittest 显示 AssertionError 但没有 Traceback
【发布时间】:2016-02-06 22:57:32
【问题描述】:

我在这里查看了其他相关问题,但没有找到我的答案。我想简化我的 Python (2.7) 单元测试的输出。尝试sys.tracebacklimit = 0 无效。

这是我的代码 sn-p(真实代码会生成很多类似的测试):

#!/usr/bin/python -E
import unittest
import os
import sys

class TestSequense(unittest.TestCase):
    pass

def test_dir_exists(dir):
    def test(self):
        self.assertTrue(os.path.isdir(dir),"ERROR: " + dir + " is not a directory")
    return test

if __name__ == '__main__':
    test = test_dir_exists("/something/not/set/correctly")
    setattr(TestSequense, "test_path",  test)
    #TODO trying remove unnecessary traceback info... still not working
    sys.tracebacklimit = 0
    unittest.main()

目前的输出是:

F
======================================================================
FAIL: test_path (__main__.TestSequense)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./simple_unittest.py", line 11, in test
    self.assertTrue(os.path.isdir(dir),"ERROR: " + dir + " is not a directory")
AssertionError: ERROR: /something/not/set/correctly is not a directory

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

我希望它看起来像这样:

F
======================================================================
FAIL: test_path (__main__.TestSequense)
----------------------------------------------------------------------
AssertionError: ERROR: /something/not/set/correctly is not a directory

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

在不解析输出的情况下这可能吗? Traceback 没有给我任何有用的信息,我正在运行 1000 次测试。

提前致谢!

【问题讨论】:

  • 不是解决方案,但回溯会告诉您哪个断言引发了错误。它使您可以直接转到发生断言失败的行,而不必考虑哪个断言会生成该消息。特别是如果您不使用断言消息或多个断言具有相同的消息,这真的很有帮助。你可能会后悔删除它。
  • 谢谢,但我总是知道它来自哪一行 - 测试套件的第一次迭代只有一行它可以来自。如果它变得更复杂,我会添加一个详细的选项

标签: python unit-testing


【解决方案1】:

我不确定是否可以使用 vanilla unittest 模块。但是你应该看看py.test,你可以使用--tb开关配置回溯中显示的信息量。

你可能感兴趣

py.test --tb=line    # only one line per failure

有关选项的完整列表,请参阅 this page

【讨论】:

  • 最佳答案,需要点赞,tb 的选项有:autolongshortnolinenative
【解决方案2】:

unittest 有一种机制可以在回溯中隐藏TestCase.assert* 方法的内容,因为它们实际上不包含任何有用的失败信息。它在框架的全局变量中查找__unittest。 您可以通过将 __unittest = True 放在模块顶部来隐藏整个模块。

【讨论】:

  • 此答案的简化版本:将 __unittest = True 放在包含测试用例的文件顶部。
【解决方案3】:

诀窍是捕获异常,去掉不需要的位并再次抛出它......

基于this 答案 - 以下代码有效...

#!/usr/bin/python -E
import unittest
import os
import sys

class TestSequense(unittest.TestCase):
    pass

def test_dir_exists(dir):
    def test(self):
       try:
           self.assertTrue(os.path.isdir(dir),"ERROR: " + dir + " is not a directory")
       except:
           # Remove traceback info as we don't need it
           unittest_exception = sys.exc_info()
           raise unittest_exception[0], unittest_exception[1], unittest_exception[2].tb_next
    return test

if __name__ == '__main__':
    test = test_dir_exists("/something/not/set/correctly")
    setattr(TestSequense, "test_path",  test)
    unittest.main()

并生成以下...

./simple_unittest.py
F
======================================================================
FAIL: test_path (__main__.TestSequense)
----------------------------------------------------------------------
AssertionError: ERROR: /something/not/set/correctly is not a directory

----------------------------------------------------------------------
Ran 1 test in 0.000s

【讨论】:

    猜你喜欢
    • 2015-01-13
    • 1970-01-01
    • 2023-03-09
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多