Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 960
infoadmin12345 Avatar asked Oct 06 '22 20:10

infoadmin12345


2 Answers

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()
    
like image 155
Aaron Digulla Avatar answered Oct 08 '22 09:10

Aaron Digulla


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
like image 31
ytoledano Avatar answered Oct 08 '22 10:10

ytoledano