Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Computing test methods in unittest.TestCase

Reply
Thread Tools

Computing test methods in unittest.TestCase

 
 
Jan Decaluwe
Guest
Posts: n/a
 
      03-02-2004
I'm working on a unit test for a finite state machine (FSM). The FSM
behavior is specified in a dictionary called transitionTable. It has a
key per state with a tuple of possible transitions as the corresponding
value. A transition is defined as a number of input values, a next
state, and a documentation string.

I want to test each possible transition in a separate test method. For
a particular transition, a test method could look as follows:

class FSMTest(unittest.TestCase):
....
def test_START_2(self):
"""Check state START - Switched channels before data started"""
state, transition = START, transitionTable[START][2]
sim = Simulation(self.bench(state, transition))
sim.run()

As the number of transitions can be large, I want to
"compute" the test methods, based on the transitionTable info.
Currently, I have the following:

class FSMTest(unittest.TestCase):
....
for st, trs in transitionTable.items():
for i, tr in enumerate(trs):
def _tmpfunc(self, st=st, tr=tr):
sim = Simulation(self.bench(st, tr))
sim.run()
_tmpfunc.func_doc = "Check state %s - %s" % (st, getDoc(tr))
exec "test_%s_%s = _tmpfunc" % (st, i)

This works, but uses some "ugly" tricks:

* default arguments to pass context info. As the code is executed in
class context, not function context, I cannot use free variables.
* The use of 'exec'. unittest looks for methods with name prefix
'test_' in the class namespace, and I didn't find another way
to achieve that.

Anyone with better ideas?

--
Jan Decaluwe - Resources bvba - http://jandecaluwe.com
Losbergenlaan 16, B-3010 Leuven, Belgium
Python is fun, and now you can design hardware with it:
http://jandecaluwe.com/Tools/MyHDL/Overview.html

 
Reply With Quote
 
 
 
 
James Kew
Guest
Posts: n/a
 
      03-02-2004

"Jan Decaluwe" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>> class FSMTest(unittest.TestCase):

> ....
> for st, trs in transitionTable.items():
> for i, tr in enumerate(trs):
> def _tmpfunc(self, st=st, tr=tr):
> sim = Simulation(self.bench(st, tr))
> sim.run()
> _tmpfunc.func_doc = "Check state %s - %s" % (st, getDoc(tr))
> exec "test_%s_%s = _tmpfunc" % (st, i)
>
> This works, but uses some "ugly" tricks:
>
> * default arguments to pass context info. As the code is executed in
> class context, not function context, I cannot use free variables.
> * The use of 'exec'. unittest looks for methods with name prefix
> 'test_' in the class namespace, and I didn't find another way
> to achieve that.
>
> Anyone with better ideas?


Define the test methods after defining the class, and inject them into the
class with setattr:

class testClass:
pass

def makeTest(param):
def test(self):
print "test: param=%s" % param
return test

setattr(testClass, "test1", makeTest(1))
setattr(testClass, "testHello", makeTest("Hello"))

>>> t = testClass()
>>> t.test1()

test: param=1
>>> t.testHello()

test: param=Hello

James


 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      03-02-2004
Jan Decaluwe wrote:

> I'm working on a unit test for a finite state machine (FSM). The FSM
> behavior is specified in a dictionary called transitionTable. It has a
> key per state with a tuple of possible transitions as the corresponding
> value. A transition is defined as a number of input values, a next
> state, and a documentation string.
>
> I want to test each possible transition in a separate test method. For


[...]

> As the number of transitions can be large, I want to
> "compute" the test methods, based on the transitionTable info.


[...]

> Anyone with better ideas?


Use a test suite instead and turn each FSMTest.testXXX() method into a
TestCase instance:

class TransitionTest(unittest.TestCase):
def __init__(self, state, transition):
unittest.TestCase.__init__(self)
self.state = state
self.transition = transition

def bench(self, state, transition):
pass

def runTest(self):
Simulation(self.bench(self.state, self.transition)).run()

def __str__(self):
return "Check state %s - %s" % (self.state, getDoc(self.transition))

def makeSuite():
suite = unittest.TestSuite()
for st, trs in transitionTable.iteritems():
for tr in trs:
suite.addTest(TransitionTest(st, tr))
return suite

if __name__ == "__main__":
unittest.main(defaultTest="makeSuite")

Peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Optical Computing: special issue - Natural Computing, Springer optical supercomputing C Programming 0 01-16-2009 12:18 PM
Optical Computing: special issue - Natural Computing, Springer optical supercomputing C Programming 0 12-19-2008 12:22 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Razer Tarantula Gaming Keyboard Review at XYZ Computing at XYZ Computing Silverstrand Front Page News 0 11-01-2006 05:13 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments