2015-11-05 22 views
3

我已經看過其他相關的問題,但還沒有找到我的答案。我想簡化我的Python(2.7)單元測試的輸出。嘗試sys.tracebacklimit = 0沒有奏效。使Python單元測試顯示AssertionError但沒有回溯

這裏是我的代碼片段(真正的代碼產生大量的類似測試):

#!/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次測試。

在此先感謝!

+0

不是一個解決方案,但追溯告訴你哪個斷言拋出錯誤。它可以讓您直接進入發生斷言失敗的行,而不必考慮哪個斷言會產生該消息。特別是如果你不使用斷言消息或多個斷言具有相同的消息,這是非常有用的。你可能會後悔去除它。 – user2357112

+0

謝謝,但我總是知道它來自哪條線 - 測試套件的第一次迭代只有一條線可以來自。如果它變得更復雜,我會添加一個詳細選項 – internetscooter

回答

0

我不確定是否可以用vanilla unittest模塊。但你應該看看py.test,利用它你可以配置--tb交換機回溯中顯示的信息量。

也許你有興趣

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

的選項的完整列表,請參閱this page

0

訣竅是捕獲異常,去掉不需要的位,並再次把它...

基於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 
相關問題