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.
While the unittest module has no options for that, there are several ways to achieve this:
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()
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
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)))]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.