python nosetests setting the test description

你离开我真会死。 提交于 2019-12-05 22:47:17

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)

You should get:

$ nosetests doc_test.py -v
test with param=-1 ... FAIL
test with param=1 ... ok
test with param=2 ... ok

You will need to change method name as well as doc string in order to populate your class properly.

EDIT: func_name should have self as an argument, since it is a class method now.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!