Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Testing how to run parameterised Testcases and pass a parameter to setupClass

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)
like image 358
renzop Avatar asked Mar 03 '16 13:03

renzop


People also ask

How do you use parameterized in Python?

Examples of Parameterized Functions in Python: Parameter exist only with in the function. Hence, we can use same name for the parameter present in function definition and any other variable outside function. In below example, we have used a parameter 'num'.

How do you parameterize a test case?

There are five steps that you need to follow to create a parameterized test. Annotate test class with @RunWith(Parameterized. class). Create a public static method annotated with @Parameters that returns a Collection of Objects (as Array) as test data set.


2 Answers

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.

like image 92
David Wolever Avatar answered Oct 09 '22 22:10

David Wolever


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)
like image 42
renzop Avatar answered Oct 09 '22 22:10

renzop