473,387 Members | 1,486 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,387 software developers and data experts.

Computing test methods in unittest.TestCase

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

Jul 18 '05 #1
3 1963

"Jan Decaluwe" <ja*@jandecaluwe.com> wrote in message
news:40**************@jandecaluwe.com...
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
Jul 18 '05 #2
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

Jul 18 '05 #3
James Kew wrote:
"Jan Decaluwe" <ja*@jandecaluwe.com> wrote in message
news:40**************@jandecaluwe.com...
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"))


Thanks - this is a nice way to get rid of the exec. However, I found
out that I still need the default arguments in my case. Free variables
do behave differently as shown in the following example:

def funcs():
flist = []
glist = []
for i in range(3):
def f():
print "f: " + str(i)
def g(i=i):
print "g: " + str(i)
flist.append(f)
glist.append(g)
for f, g in zip(flist, glist):
f(); g()
funcs()

f: 2
g: 0
f: 2
g: 1
f: 2
g: 2

When generating such functions (with free variables) in loops,
it's apparently very easy to get it wrong ...

Regards, Jan

--
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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
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...
2
by: chris | last post by:
We have a number of TestCase classes that have multiple test methods. We are interested in removing any of the individual test methods on the fly (dynamically, at runtime, whatever). We...
4
by: ajikoe | last post by:
Hello I found something strange in my unittest : This code is ok (will report error ): class MyTest1(unittest.TestCase): def runTest(self): self.assertEqual(2,3) pass
6
by: Ben Finney | last post by:
Howdy all, Summary: I'm looking for idioms in unit tests for factoring out repetitive iteration over test data. I explain my current practice, and why it's unsatisfactory. When following...
5
by: Sakcee | last post by:
Hi I am trying to use pyUnit to create a framework for testing functions I am reading function name, expected output, from a text file. and in python generating dynamic test functions...
3
by: Bruce Cropley | last post by:
Hi all I'm trying to generate test methods in a unittest TestCase subclass, using decorators. I'd like to be able to say: class MyTestCase(unittest.TestCase): @genTests(, , ) def...
2
by: Podi | last post by:
Hi, Newbie question about unittest. I am having trouble passing a variable to a test class object. MyCase class will potentially have many test functions. Any help would be much...
6
by: Vyacheslav Maslov | last post by:
Hi all! I have many many many python unit test, which are used for testing some remote web service. The most important issue here is logging of test execution process and result. I strongly...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.