unittest for threading function always failed...

i3dmaster i3dmaster at gmail.com
Fri May 18 19:13:36 EDT 2007


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




More information about the Python-list mailing list