views:

52

answers:

2

I am testing classes that parse XML and create DB objects (for a Django app). There is a separate parser/creater class for each different XML type that we read (they all create essentially the same objects). Each parser class has the same superclass so they all have the same interface.

How do I define one set of tests, and provide a list of the parser classes, and have the set of tests run using each parser class? The parser class would define a filename prefix so that it reads the proper input file and the desired result file.

I want all the tests to be run (it shouldn't stop when one breaks), and when one breaks it should report the parser class name.

+2  A: 

With nose, you can define test generators. You can define the test case and then write a test generator which will yield one test function for each parser class.

djs
+1  A: 

If you are using unittest, which has the advantage of being supported by django and installed on most systems, you can do something like:

class TestBase(unittest.TestCase)
    testing_class = None

    def setUp(self):
        self.testObject = testing_class(foo, bar)

and then to run the tests:

for cls in [class1, class2, class3]:
    testclass = type('Test'+cls.__name, (TestBase, ), {'testing_class': cls})
    suite = unittest.TestLoader().loadTestsFromTestCase(testclass)
    unittest.TextTestRunner(verbosity=2).run(suite)

I haven't tested this code but I've done stuff like this before.

aaronasterling