unittest: Dynamically creating tests

Raymond Hettinger vze4rx4y at verizon.net
Wed Jun 11 08:41:40 CEST 2003


[Jeremy Bowers]
> My problem is that I'm having trouble writing the tests in such a way that
> they execute. Consider the following simplified code that demonstrates
> what I am trying to do:
>
> -------------------
>
> import unittest
>
> tests = unittest.TestSuite()
>
> for i in range(5):
>     class NextTest(unittest.TestCase):
>         def testEquality(self):
>             self.assert_(1 == 1)
>
>     tests.addTest(NextTest)

That should be:

   tests.addTest(unittest.makeSuite(NextTest))



> if __name__ == '__main__':
>     unittest.main()

Don't use main() which expects to parse your file for class names.
Instead, substitute this line:

    unittest.TextTestRunner().run(tests)



Raymond Hettinger






More information about the Python-list mailing list