6

I have an python unitest. In the setupClass method I so some timeconsuming tasks... The tests itself run very fast. Now i would like to run the same Testcase with multiple sets of parameters. How can I achieve this?

I ve tried differet approaches with nose_parameterized etc. but there i cant use the @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)
renzop
  • 1,194
  • 2
  • 12
  • 26
  • You may use unittest.subTest context manager - see this thread http://stackoverflow.com/questions/43912153/executing-single-unittest-over-a-sequence-in-python – volcano May 11 '17 at 13:03

3 Answers3

8

Unfortunately there isn't any way to create parameterized test classes with either unittest, nose, or parameterized.

py.test has an example showing how you can build your own parameterized test class, here: https://pytest.org/latest/example/parametrize.html#a-quick-port-of-testscenarios

And you can build your own parameterized class generator like this:

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,
    })

Which will generate a new test class for each paramter.

David Wolever
  • 148,955
  • 89
  • 346
  • 502
  • 1
    Just for completeness nose-parameterized is depricated. The replacement is https://pypi.python.org/pypi/parameterized – renzop Nov 14 '17 at 08:09
2

Here is one way to do it with unittest for completeness but i prefere Davids answer.

import unittest from nose_parameterized import parameterized

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)
renzop
  • 1,194
  • 2
  • 12
  • 26
0

You can use the following approach, if you have multiple params and you can call only on method with those values:

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)

running the test will return:

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
mmsilviu
  • 1,211
  • 15
  • 25