Python unit test. How to add sleep time between test cases?

I am using the python unit test module. I am interested, anyway, to add some delay between every 2 test cases? Since my unit test just makes an http request, and I assume that the server can block a frequent request from the same ip.

+6
python unit-testing
source share
2 answers

Put to sleep inside the tearDown method of your TestCase

 class ExampleTestCase(unittest.TestCase): def setUp(self): pass def tearDown(self): time.sleep(1) # sleep time in seconds 

This will be done after each test inside TestCase

EDIT : added setUp because the documentation may indicate that you cannot have tearDown without one, but this is not clear

+11
source share
 import time time.sleep(2.5) # sleeps for 2.5 seconds 

Perhaps you should consider making the delay a random value between x and y.

+2
source share

All Articles