[英]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). 我正在测试解析XML并创建数据库对象的类(对于Django应用程序)。 There is a separate parser/creater class for each different XML type that we read (they all create essentially the same objects).
对于我们读取的每种不同的XML类型,都有一个单独的解析器/创建器类(它们都创建基本相同的对象)。 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: 如果您使用的是unittest ,它具有django支持并安装在大多数系统上的优势,您可以执行以下操作:
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. 我没有测试过这段代码,但之前我做过这样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.