简体   繁体   中英

how do I repeat python unit tests on different data?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM