6

我有一个 python unitest。在 setupClass 方法中,我做了一些耗时的任务......测试本身运行得非常快。现在我想用多组参数运行同一个测试用例。我怎样才能做到这一点?

我已经尝试过使用nose_parameterized 等不同的方法,但是我不能使用@parameterized.expand()

import unittest
from nose_parameterized import parameterized


parameters = [('test1', 2 ),('test2', 3)]


class TestParameterizedTestcase(unittest.TestCase):
    @classmethod
    def setUpClass(cls, param=1):
        """
        Do some expensive stuff
        """
        cls.param = param
        print 'Param in setup class  %s'


    def test_is_one(self):
        """
        A fast test
        """
        self.assertEqual(1,self.param)

    def test_is_two(self):
        """
        Another fast test
        """
        self.assertEqual(2, self.param)

    def test_is_three(self):
        """
        Another fast test
        """
        self.assertEqual(3, self.param)
4

3 回答 3

8

不幸的是,没有任何方法可以使用unittestnoseparameterized.

py.test有一个示例显示如何构建自己的参数化测试类,此处:https ://pytest.org/latest/example/parametrize.html#a-quick-port-of-testscenarios

您可以像这样构建自己的参数化类生成器:

class MyTestClassBase(object):
    # Inherit from `object` so unittest doesn't think these are tests which
    # should be run

    @classmethod
    def setUpClass(cls):
        print "doing expensive setup with", cls.param

    def test_one(self):
        self.assertEqual(self.param, 1)


params = [('test1', 1), ('test2', 2)]

for name, param in params:
    cls_name = "TestMyTestClass_%s" %(name, )
    globals()[cls_name] = type(cls_name, (MyTestClassBase, unittest.TestCase), {
        "param": param,
    })

这将为每个参数生成一个新的测试类。

于 2016-03-03T18:27:04.247 回答
1

这是一种使用 unittest 来确保完整性的方法,但我更喜欢 Davids 的回答。

从nose_parameterized 导入unittest 导入参数化

class TestParameterizedTestcase(unittest.TestCase):
    param =3
    @classmethod
    def setUpClass(cls):
        """
        Do some expensive stuff
        """

        print 'Param in setup class  %s' % cls.param


    def test_is_one(self):
        """
        Some fast test
        """
        self.assertEqual(1,self.param)

    def test_is_two(self):
        """
        Anoter Fast test
        """
        self.assertEqual(2, self.param)

import unittest
from unittest import TestLoader

if __name__ == '__main__':
    for param in range(5):
        suite = unittest.TestSuite()
        loader = TestLoader()
        test = None
        test = TestParameterizedTestcase
        test.param =param

        tests = loader.loadTestsFromTestCase(test)
        suite.addTest(tests)
        unittest.TextTestRunner().run(suite)
于 2016-03-04T12:56:39.640 回答
0

如果您有多个参数并且只能调用具有这些值的方法,则可以使用以下方法:

import unittest
from parameterized import parameterized

def custom_name_func(testcase_func, param_num, param):
    """
    The names of the test cases generated by @parameterized.expand
    :param testcase_func: will be the function to be tested
    :param param_num: will be the index of the test case parameters in the list of parameters
    :param param: (an instance of param) will be the parameters which will be used.
    :return: test case name
    """
    return "%s_%s" % (
        testcase_func.__name__,
        parameterized.to_safe_name("_".join([str(param.args[0]), param_num])),
    )


class SomeTestCase(unittest.TestCase):
    @parameterized.expand([
        ('ex1', 2, 3, 5),
        ('ex2', 2, 5, 7),
        ('ex3', 3, 8, 11)
    ], name_func=custom_name_func)

    def test_add(self, name, a, b, expected):
        self.assertEqual(a + b, expected)

运行测试将返回:

python -m unittest /tests/test_one.py -v      
                 
test_add_ex1_0 (tests.test_one.SomeTestCase) ... ok
test_add_ex2_1 (tests.test_one.SomeTestCase) ... ok
test_add_ex3_2 (tests.test_one.SomeTestCase) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.000s
于 2022-01-18T11:47:12.780 回答