Computing test methods in unittest.TestCase

Peter Otten __peter__ at web.de
Tue Mar 2 13:33:56 EST 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




More information about the Python-list mailing list