【发布时间】: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