Unittest - How do I code lots of simple tests

Paul Moore paul.moore at atosorigin.com
Wed Oct 22 06:11:30 EDT 2003


Peter Hansen <peter at engcorp.com> wrote in message news:<3F95A5DC.51828269 at engcorp.com>...
> Paul Moore wrote:
> > 
> > Can anyone suggest a more reasonable way of running this sort of
> > table-driven test via unittest?
> 
> Why not just extend self.assertEqual() and use your own check, with
> additional logic as required to increment counters or add items
> to the list of passing tests.  Then put a final check of the number
> of passing tests or something like that at the end to make sure 
> things worked overall.
> For example:
> 
> class KnownValues(unittest.TestCase):
>     def setUp(self):
>         self.passCount = 0
> 
>     def checkValue(self, expected, result):
>         if expected == result:
>             self.passCount += 1
>         else:
>             # left as exercise to the reader, but pass would work...
> 
>     def testToRomanKnownValues(self):
>         for integer, numeral in self.knownValues:
>             result = roman.toRoman(integer)
>             self.checkValue(numeral, result)
>         self.assertEqual(len(self.knownValues), self.passCount)
> 
> No, you don't get the psychologically affirming "52 tests passed!"
> without changes to the TestRunner, but I assume the non-cosmetic part
> of this is more your concern right now...

I don't see the remainder of the problem as "non-cosmetic". The error
report I get (or rather the information it offers) is

    1 test failed - pass count is 25 instead of 52.

But that doesn't tell me *which* tests failed.

The key point here is that I'm NOT running one test - I really am
running 52 distinct tests. OK, they are all very similar, differing
only in the data - but for pity's sake, isn't that what an object
oriented structure is supposed to make easy???

Paul.




More information about the Python-list mailing list