Python unittest: Can we repeat Unit test case execution for a configurable number of times?
Asked Answered
P

5

7

Can we repeat Unit test case execution for a configurable number of times?

For example, I have a Unit test script named Test_MyHardware that contains couple of test cases test_customHardware1 and test_customHardware2.

Is there a way to repeat the execution of test_customHardware1 200 times and test_customHardware2 500 times with Python's unittest module?

Note: The above case presented is simplified. In practise, we would have 1000s of test cases.

Pugh answered 28/11, 2012 at 13:1 Comment(1)
I
3

While the unittest module has no options for that, there are several ways to achieve this:

  • You can (ab)use the timeit module to repeatedly calling a test method. Remember: Test methods are just like normal methods and you can call them yourself. There is no special magic required.
  • You can use decorators to achieve this:

    #!/usr/bin/env python
    
    import unittest
    
    def repeat(times):
        def repeatHelper(f):
            def callHelper(*args):
                for i in range(0, times):
                    f(*args)
    
            return callHelper
    
        return repeatHelper
    
    
    class SomeTests(unittest.TestCase):
        @repeat(10)
        def test_me(self):
            print "You will see me 10 times"
    
    if __name__ == '__main__':
        unittest.main()
    
Iggie answered 28/11, 2012 at 13:21 Comment(1)
Note that this approach won't work if you have something going on in setUpAfoul
T
2

A better option is to call unittest.main() multiple times with exit=False. This example takes the number of times to repeat as an argument and calls unittest.main that number of times:

parser = argparse.ArgumentParser()
parser.add_argument("-r", "--repeat", dest="repeat", help="repeat tests")
(args, unitargs) = parser.parse_known_args()
unitargs.insert(0, "placeholder") # unittest ignores first arg
# add more arguments to unitargs here
repeat = vars(args)["repeat"]
if repeat == None:
    repeat = 1
else:
    repeat = int(repeat)
for iteration in range(repeat):
    wasSuccessful = unittest.main(exit=False, argv=unitargs).result.wasSuccessful()
    if not wasSuccessful:
        sys.exit(1)

This allows much greater flexibility since it will run all tests the user requested the number of times specified.

You'll need to import:

import unittest
import argparse
Tophole answered 20/10, 2016 at 9:54 Comment(0)
R
0

Adding these 2 lines of code helped me. Multiple tests can be added in addition to 'test_customHardware1' in below array.

repeat_nos = 10
unittest.TextTestRunner().run(unittest.TestSuite (map (Test_MyHardware, ['test_customHardware1' ]*repeat_nos)))]
Rucksack answered 17/8, 2022 at 18:42 Comment(0)
S
0

Can make use parameterized to repeat a specific test:

    from parameterized import parameterized

    @parameterized.expand([0,1,2,3,4,5,6,7,8,9])
    def test_my_code(self, run):
       # ...
Streeter answered 20/8 at 13:51 Comment(0)
M
0

To go further than this answer, a decorator that calls setUp and tearDown:

def repeat_test(n: int):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(self):
            for i in range(n):
                if i != 0:
                    self.setUp()
                func(self)
                if i != n - 1:
                    self.tearDown()
        return wrapper
    return decorator

Can be used as:

class SomeTest(unittest.TestCase):
    @repeat_test(8)
    def my_test_case(self):
        pass
Mensural answered 4/9 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.