2012-07-11 73 views
4

我正在寫與鼻子的測試套裝,想失敗的情況下,顯示一個類似如何使用鼻子來抑制失敗測試用例的痕跡?

輸出「失敗:is_even(5):甚至沒有」

而不是默認的輸出:

====================================================================== 
FAIL: seed_db.test_generator(5,) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/usr/local/lib/python2.7/dist-packages/nose/case.py", line 197, in runTest 
    self.test(*self.arg) 
    File "/home/apurcell/tests/prism/seed_db.py", line 59, in is_even 
    nose.tools.eq_(x % 2, 0, msg="Not even") 
    File "/usr/local/lib/python2.7/dist-packages/nose/tools.py", line 31, in eq_ 
    assert a == b, msg or "%r != %r" % (a, b) 
    AssertionError: Not even 

---------------------------------------------------------------------- 

有沒有可以做到這一點的鼻子的選項?

回答

0

一個可能的解決方案是將錯誤流重定向到類似對象的流中,並處理其中的不同輸出。這可能看起來像下面的代碼片段:

import sys 

class MyErrorStream(): 
    def write(self, txt): 
     # Do something if 
     # output contains 
     # special exception 
     altered_txt = 'My special exception is:\n' + txt 

     sys.__stderr__.write(altered_txt) 

if(__name__ == '__main__'): 
    error_stream = MyErrorStream() 
    sys.stderr = error_stream 

    assert(1 == 0) 

但是,這種解決方案不是最好的解決方案。另一種改變堆棧跟蹤的方法是修改處理輸出的內部類鼻子。您可以對它進行子類化並覆蓋/擴展創建輸出文本的方法。因爲我沒有使用鼻子,所以我不能給出一個最小的代碼片段。不過我希望我能幫助你。

+0

有沒有人找到解決方案呢? – user2921139 2014-10-29 23:37:22

2

如果你想改變鼻子行爲,你應該寫一個插件(see API documentation here)。就你而言,這聽起來像你想改變錯誤報告的方式,所以你想提供formatError()formatFailure()。可能您希望編輯異常消息(以包含行號),並限制回溯的大小。

0

以下輸出與您想要的類似但不完全相同(並且它也會更改成功的測試輸出,這可能不是您想要的)。它使用tap.py來輸出TAP(測試任何協議),而不是通常的單元測試輸出。

nosetests --with-tap --tap-stream testcases-rsysflow.py 

輸出看起來像:

[email protected]$ nosetests testcases-rrrr.py --with-tap --tap-stream 
# TAP results for TestThing 
ok 1 - test_create_and_get (testcases-rrrr.TestThing) 
ok 2 - test_del_one (testcases-rrrr.TestThing) 
ok 3 - test_get_all (testcases-rrrr.TestThing) 
not ok 4 - test_get_not_exist (testcases-rrrr.TestThing) 
ok 5 - test_get_wrong_indir (testcases-rrrr.TestThing) 
ok 6 - test_replace_and_get (testcases-rrrr.TestThing) 
ok 7 - test_set_should_fail (testcases-rrrr.TestThing) 
1..7 

隨着測試任何協議(我所說的協議,沒有這種具體實施的),你可以在錯誤破折號後輸出的診斷信息 - 但我不知道該如何做到這一點。

這個實現很方便,因爲我只需安裝tap.py(pip install tap.py)並將這兩個命令行參數放在我的unittest測試的nosetest調用和poof-TAP格式化輸出上。現在可以將它插入Jenkins。

相關問題