By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

unittest for threading function always failed...

P: n/a
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

May 18 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On May 18, 4:13 pm, i3dmaster <i3dmas...@gmail.comwrote:
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...

May 19 '07 #2

P: n/a

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

May 21 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.