2014-06-17 92 views
3

我在飞行创建测试(我要)在python与如下nosetests运行:蟒蛇nosetests设置测试描述

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 

的问题是,nosetest打印:

make tests on the fly ... ok 
make tests on the fly ... ok 

这不是我想要的。我想要输入鼻子测试说:

test with param=1 ... ok 
test with param=2 ... ok 

任何想法?

+0

nosetest生成器功能通常显示您传递的参数,不知道为什么它不适合您的代码。 – schlenk

回答

2

这里是如何做你想做的,但它会绕过yield测试代。从本质上说,你在飞行的东西使用空白unittest.TestCase下面populate()方法:

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 

您需要以填充改变方法的名称,以及文档字符串你的班级正确。

编辑:func_name应该有self作为参数,因为它现在是一个类方法。

+0

非常感谢。这正是我的意思。 – max