4

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 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is what ended up working for me (I ended up converting from unittest tests to nose tests). If you go with this approach, make sure you import with_setup (from nose.tools import with_setup) for the @with_setup decorator to work correctly, if you want setup/teardown fixtures running alongside your tests.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.