How to organize test cases with PyUnit

Frank Niessink niessink at serc.nl
Sat Jul 13 17:42:30 EDT 2002


Roy Smith <roy at panix.com> wrote:
> 
> Peter Hansen <peter at engcorp.com> wrote:
>> Is it really important the order the tests are run in? 
> 
> I'm not sure, but it certainly *feels* like it should be important ;-)
> 
> Well, here's an example.  I've got a parser which returns a dictionary 
> of dictionaries.  My setUp() method put this in self.tables, then I've 
> got:
> 
>    def test101 (self):
>        'parse() returns a dictionary'
>        self.assertEqual (type (self.tables), DictType)
> 
>    def test102 (self):
>        'parse() returns a dictionary of dictionaries'
>        for table in self.tables.values():
>            self.assertEqual (type (table), DictType)

I would probably do it like this:

def testTableTypes(self):
    self.assertEqual (type (self.tables), DictType)
    for table in self.tables.values():
        self.assertEqual (type (table), DictType)

This way the asserts in the for loop are only tested if the first assert
succeeds.

Maybe I would even make a custom assert, to make the code a bit more
readable:

def assertTypeIsDict(self, object):
    self.assertEqual(type(object), DictType)

def testTableTypes(self):
    self.assertTypeIsDict(self.tables)
    for table in self.tables.values():
        self.assertTypeIsDict(table)


Cheers, Frank
-- 
Important facts from Galactic history, number one: (Reproduced from the 
Siderial Daily Mentioner's Book of popular Galactic History.) The night sky
over the planet Krikkit is the least interesting sight in the entire Universe.
	-- Douglas Adams, 'Life, the Universe, and Everything'



More information about the Python-list mailing list