M.K*_*.K. 8 python unit-testing generator nose nosetests
Nose有一个bug - 生成器创建的测试名称没有被缓存,因此错误看起来像是在上一次测试中发生的,而不是它失败的实际测试.我在bug报告讨论中解决了这个问题,但它只适用于stdout上显示的名称,而不适用于XML报告(--with-xunit)
from functools import partial, update_wrapper
def testGenerator():
for i in range(10):
func = partial(test)
# make decorator with_setup() work again
update_wrapper(func, test)
func.description = "nice test name %s" % i
yield func
def test():
pass
Run Code Online (Sandbox Code Playgroud)
鼻子的输出正如预期的那样
nice test name 0 ... ok
nice test name 1 ... ok
nice test name 2 ... ok
...
Run Code Online (Sandbox Code Playgroud)
但XML中的测试名称只是'testGenerator'.
...<testcase classname="example" name="testGenerator" time="0.000" />...
Run Code Online (Sandbox Code Playgroud)
如何更改此设置,以便在stdout和XML输出上显示个性化测试名称?
我正在使用nosetests版本1.1.2和Python 2.6.6
您可以通过添加实现describeTest的插件来更改Nose命名测试的方式
from nose.plugins import Plugin
class CustomName(Plugin):
"Change the printed description/name of the test."
def describeTest(self, test):
return "%s:%s" % (test.test.__module__, test.test.description)
Run Code Online (Sandbox Code Playgroud)
然后,您必须安装此插件,并在 Nose 调用中启用它。