Q:

Python unit test. How to add some sleeping time between test cases?

I am using python unit test module. I am wondering is there anyway to add some delay between every 2 test cases? Because my unit test is just making http request and I guess the server may block the frequent request from the same ip.

A:

Put a sleep inside the tearDown method of your TestCase
import time

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

tearDown() will be executed after every test within that TestCase class.
The modules documentation can be found here.

A:

import time
time.sleep(2.5) # sleeps for 2.5 seconds

You might want to consider making the delay a random value between x and y.

