Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python测试设置测试描述_Python_Nosetests - Fatal编程技术网

python测试设置测试描述

python测试设置测试描述,python,nosetests,Python,Nosetests,我正在用python动态(必须)创建测试,以便使用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__ = "t

我正在用python动态(必须)创建测试,以便使用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
问题在于,鼻子测试显示:

make tests on the fly ... ok
make tests on the fly ... ok
这不是我想要的。我希望测试的输出显示:

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

有什么想法吗?

这里是如何做你想做的事情,但是它将绕过
产生
测试生成。本质上,您可以使用下面的
populate()
方法动态填充一个空白的
unittest.TestCase

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
为了正确地填充类,您需要更改方法名和文档字符串


EDIT:func\u name应该有
self
作为参数,因为它现在是一个类方法。

nosetest生成器功能通常会显示您通过的参数,但不确定为什么您的代码没有通过。非常感谢。这正是我的意思。