unittest for threading function always failed...

I

i3dmaster

I am having a little difficulty to figure out why this unittest for a
Thread subclass always fails...


# unittest code:

class SPThreadUnitTest(unittest.TestCase):

def testgetresult(self):
from random import randint
self.i = randint(1,10)
def p(n): return n
self.t = spthread.SPThread(target=p, args=(self.i))
self.t.start()
#self.t._res = self.t._target(self.t._args)
self.assertEquals(self.i,self.t.getresult())


#spthread.SPThread code:

import threading
class SPThread(threading.Thread):

def __init__(self,target=None,args=None):
threading.Thread.__init__(self)
self._target = target
self._args = args
self._res = None

def getresult(self):
return self._res

def run(self):
self._res = self._target(self._args)


A simple little test. But no matter what, the self._res didn't get any
value but None, so the assertion of self.i and self.t.getresult()
always fails. If I use the commented out code, it works. So the
start() function has some tricky stuff? Can someone point me out where
the problem is?

Thanks,
Jim
 
I

i3dmaster

I am having a little difficulty to figure out why this unittest for a
Thread subclass always fails...

# unittest code:

class SPThreadUnitTest(unittest.TestCase):

def testgetresult(self):
from random import randint
self.i = randint(1,10)
def p(n): return n
self.t = spthread.SPThread(target=p, args=(self.i))
self.t.start()
#self.t._res = self.t._target(self.t._args)
self.assertEquals(self.i,self.t.getresult())

#spthread.SPThread code:

import threading
class SPThread(threading.Thread):

def __init__(self,target=None,args=None):
threading.Thread.__init__(self)
self._target = target
self._args = args
self._res = None

def getresult(self):
return self._res

def run(self):
self._res = self._target(self._args)

A simple little test. But no matter what, the self._res didn't get any
value but None, so the assertion of self.i and self.t.getresult()
always fails. If I use the commented out code, it works. So the
start() function has some tricky stuff? Can someone point me out where
the problem is?

Thanks,
Jim

oh wft... I got it now. its the join() call...
 
K

Kathryn Van Stone

If all you are doing is testing that run() works correctly, you could
probably also get away with just calling run() directly instead of
also implicitly testing the Thread class as well.

-Kathy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top