小编int*_*ter的帖子

使Python单元测试显示AssertionError但没有Traceback

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

这是我的代码片段(实际代码会生成许多类似的测试):

#!/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()
Run Code Online (Sandbox Code Playgroud)

当前输出为:

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 + " …
Run Code Online (Sandbox Code Playgroud)

python unit-testing

5
推荐指数
2
解决办法
2807
查看次数

标签 统计

python ×1

unit-testing ×1