[Python-ideas] Minor suggestion for unittest
Collin Winter
collinw at gmail.com
Thu Apr 26 17:45:01 CEST 2007
On 4/25/07, Terry Jones <terry at jon.es> wrote:
> import unittest
>
> class Test(unittest.TestCase):
> def runTest(): pass
>
> suite = unittest.TestSuite()
>
> for testFunc, expectedResult in MyTestData:
> newTestFuncName = 'dynamic-test-' + testFunc.__name__
> def tester():
> self.assertEqual(testFunc(), expectedResult)
> test = Test()
> setattr(test, newTestFuncName, tester)
> # Set the class instance up so that it will be the one run.
> test.__init__(newTestFuncName) # ugh!
> suite.addTest(test)
>
> suite.run()
It sounds like what you're looking for is FunctionTestCase
(http://docs.python.org/lib/unittest-contents.html). Using that, your
loop above becomes something like
for testFunc, expectedResult in MyTestData:
def tester():
self.assertEqual(testFunc(), expectedResult)
suite.addTest(FunctionTestCase(tester))
Collin Winter
More information about the Python-ideas
mailing list