使 Python unittest 显示 AssertionError 但不显示 Traceback [英] Make Python unittest show AssertionError but no Traceback
问题描述
我在这里查看了其他相关问题,但没有找到我的答案.我想简化我的 Python (2.7) 单元测试的输出.尝试 sys.tracebacklimit = 0
不起作用.
I have looked at the other related questions here but have not found my answer. I would like to simplify the output of my Python (2.7) unittests. Trying sys.tracebacklimit = 0
did not work.
这是我的代码片段(真实代码生成了很多类似的测试):
Here is my code snippet (the real code generates lots of similar tests):
#!/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)
我希望它看起来像这样:
I would like it to look like this:
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)
这可能不解析输出吗?回溯没有给我任何有用的信息,我正在运行 1000 次测试.
Is this possible without parsing the output? The Traceback gives me no useful information and I am running 1000's of tests.
提前致谢!
推荐答案
我不确定是否可以使用 vanilla unittest 模块.但是你应该看看py.test,用它你可以配置显示的信息量在带有 --tb
开关的回溯中.
I'm not sure if it's possible with vanilla unittest module. But you should take a look at py.test, with it you can configure the amount of information shown in a traceback with the --tb
switch.
可能你感兴趣
py.test --tb=line # only one line per failure
请参阅此页面以获取完整的选项列表.
See this page for a full list of options.
这篇关于使 Python unittest 显示 AssertionError 但不显示 Traceback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!