[TIP] How do I run the same test on many different classes?
Michael Foord
fuzzyman at voidspace.org.uk
Sun Dec 13 12:32:22 PST 2009
On 13/12/2009 20:07, Matthew Wilson wrote:
> On Sun, Dec 13, 2009 at 3:02 PM, ssteinerX at gmail.com
> <ssteinerx at gmail.com> wrote:
>
>> On Dec 13, 2009, at 2:45 PM, Matthew Wilson wrote:
>>
>>
>>> This is one of those problems that I know how to solve in an ugly way,
>>> but I want to learn a better way.
>>>
>> What's the ugly way?
>>
> Copy and paste my TestTask for each Task1, Task2, etc, like this:
>
> class TestTask1(TestTask):
> def setUp(self):
> self.t = Task1()
>
> class TestTask2(TestTask):
> def setUp(self):
> self.t = Task2()
>
>
> Seems a little ironic to me that it is so hard to pass in a parameter
> to a test, since using parameters rather than making objects
> internally is so useful for writing testable code :)
>
You could at least build this with a class factory:
import imp
mod = imp.new_module('test_tasks')
from testcase import TestCase
def make_test(task):
class TheTest(TestCase):
def setUp(self):
self.t = task()
return TheTest
for index, task in enumerate([Task1, Task2, Task3...]):
setattr(mod, 'TestClass%s' % index, make_test(task)
I believe that the Bzr guys (?) do this by copying TestCase classes.
All the best,
Michael
> Matt
>
>
--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog
More information about the testing-in-python
mailing list