lundi 3 août 2015

time.sleep() in unit test Python when using threads

I'm learning threads in Python and thought unit testing would fit my needs.

Using this http://ift.tt/1eyN8Lr as my point of origin. However when i'm using time.sleep() in my function, the test seems to return from that code block.

def test_threads(self):
    thread.start_new_thread(self.print_time,  ("Thread 1", 1))

def print_time(self, threadName, delay):
    count = 0   
    while count < 5:
        time.sleep(delay)
        count += 1
        print "%s: %s" % (threadName, time.ctime(time.time()))

Using this code outputs nothing. Getting rid of time.sleep(delay) outputs:

Thread 1: Mon Aug 03 11:36:56 2015
Thread 1: Mon Aug 03 11:36:56 2015
Thread 1: Mon Aug 03 11:36:56 2015
Thread 1: Mon Aug 03 11:36:56 2015
Thread 1: Mon Aug 03 11:36:56 2015

How can I use time.sleep() when i'm using unit test in Python?

Aucun commentaire:

Enregistrer un commentaire