[Python-ideas] Minor suggestion for unittest

Terry Jones terry at jon.es
Sat Apr 28 15:56:56 CEST 2007

Hi Collin

Thanks for the reply.

| 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))

I had read about FunctionTestCase but it didn't seem to be what I was
looking for - though it's the closest. FunctionTestCase is intended to
allow people to easily bring a set of pre-existing tests under the umbrella
of unittest. It overrides setUp and tearDown, and doesn't result in the
test being a first-class test like those you get when you write tests for
unittest from scratch (using TestCase directly, or something you write
based on it).

I want to dynamically (i.e. at run time) add functions that are treated
equally with those that are added statically in python code. That could be
really simple (and I can hack around it to achieve it), but the current
method unittest uses to set its self._testMethodName prevents me from doing
this in a nice way (because TestCase.__init__ immediately does a hasattr to
look for the named method, and fails if it's absent).

I wonder if I'm being clear... it's pretty simple, but my explanation may
not be so good.


More information about the Python-ideas mailing list