Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 837
zs2020 Avatar asked Apr 15 '10 19:04

zs2020


2 Answers

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.

like image 69
Daniel DiPaolo Avatar answered Nov 09 '22 13:11

Daniel DiPaolo


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.

like image 29
ChristopheD Avatar answered Nov 09 '22 12:11

ChristopheD