473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

unittest for threading function always failed...

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
2 3423
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Will Stuyvesant | last post by:
I have a unittest testfile like this: ----------------------- test_mod.py --------------------- import sys sys.path.append('..') import unittest import mod class...
12
by: Paul Moore | last post by:
One of the things I really dislike about Unittest (compared, say, to a number of adhoc testing tricks I've used in the past, and to Perl's "standard" testing framework) is that the...
8
by: Remy Blank | last post by:
Hello unittest users, In a project I am working on, I have a series of tests that have to be run as root, and others as a normal user. One solution is to separate the tests into two different...
0
by: Remy Blank | last post by:
Ok, here we go. I added the possibility for tests using the unittest.py framework to be skipped. Basically, I added two methods to TestCase: TestCase.skip(msg): skips unconditionally...
1
by: Thomas Heller | last post by:
I'm trying to integrate some doctest tests with unittest. The tests must be exposed as one or more subclasses of unittest.TestCase, so I'm collecting them with a call to doctest.DocTestSuite(),...
41
by: Roy Smith | last post by:
I've used the standard unittest (pyunit) module on a few projects in the past and have always thought it basicly worked fine but was just a little too complicated for what it did. I'm starting a...
5
by: paul kölle | last post by:
hi all, I noticed that setUp() and tearDown() is run before and after *earch* test* method in my TestCase subclasses. I'd like to run them *once* for each TestCase subclass. How do I do that. ...
3
by: David Vincent | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hello I'm hoping to get some insight into a situation that seems odd to me. My Python experience is limited; I've just started using the unittest module....
2
by: Oleg Paraschenko | last post by:
Hello, I decided to re-use functionality of "unittest" module for my purposes. More precisely, I have a list of folders. For each folder, code should enter to the folder, execute a command and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.