python鼻子测试设置测试描述 [英] python nosetests setting the test description
问题描述
我正在(必须)在python中动态创建测试以运行如下所示的鼻子测试:
I'm creating tests on the fly (I must) in python to run with nosetests as below:
def my_verification_method(param):
""" description """
assert param>0, "something bad..."
def test_apps():
""" make tests on the fly """
param1 = 1
my_verification_method.__doc__ = "test with param=%i" % param1
yield my_verification_method, param1
param1 = 2
my_verification_method.__doc__ = "test with param=%i" % param1
yield my_verification_method, param1
问题是鼻子测试打印:
make tests on the fly ... ok
make tests on the fly ... ok
这不是我想要的.我想要鼻子测试的输出说:
which is not what I want. I want the output of nosetests say:
test with param=1 ... ok
test with param=2 ... ok
有什么想法吗?
推荐答案
这是您要执行的操作,但是它将绕过yield
测试生成.本质上,您可以使用下面的populate()
方法动态填充空白的unittest.TestCase
:
Here is how to do what you want, but it will be bypassing yield
test generation. In essence, you stuff on the fly a blank unittest.TestCase
using populate()
method below:
from unittest import TestCase
from nose.tools import istest
def my_verification_method(param):
""" description """
print "this is param=", param
assert param > 0, "something bad..."
def method_name(param):
""" this is how you name the tests from param values """
return "test_with_param(%i)" % param
def doc_name(param):
""" this is how you generate doc strings from param values """
return "test with param=%i" % param
def _create(param):
""" Helper method to make functions on the fly """
@istest
def func_name(self):
my_verification_method(param)
return func_name
def populate(cls, params):
""" Helper method that injects tests to the TestCase class """
for param in params:
_method = _create(param)
_method.__name__ = method_name(param)
_method.__doc__ = doc_name(param)
setattr(cls, _method.__name__, _method)
class AppsTest(TestCase):
""" TestCase Container """
pass
test_params = [-1, 1, 2]
populate(AppsTest, test_params)
您应该得到:
$ nosetests doc_test.py -v
test with param=-1 ... FAIL
test with param=1 ... ok
test with param=2 ... ok
您将需要更改方法名称以及doc字符串,以便正确填充您的类.
You will need to change method name as well as doc string in order to populate your class properly.
func_name应该以self
作为参数,因为它现在是一个类方法.
func_name should have self
as an argument, since it is a class method now.
这篇关于python鼻子测试设置测试描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!