2012-07-11 27 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。

相关问题