5

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.

user
  • 5,370
  • 8
  • 47
  • 75
infoadmin12345
  • 211
  • 1
  • 4
  • 10

3 Answers3

2

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()
    
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
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
ytoledano
  • 3,003
  • 2
  • 24
  • 39
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)))]
RAKESH
  • 1
  • 1