简体   繁体   English

如何对不同的数据重复python单元测试?

[英]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.

相关问题 如何在 Python 单元测试中模拟文件系统? - How do I mock the filesystem in Python unit tests? 如何使用nosetests测量python单元测试的执行时间? - How do I measure the execution time of python unit tests with nosetests? 如何在目录中运行所有 Python 单元测试? - How do I run all Python unit tests in a directory? 如何使python单元测试总是在从不同的工作目录运行时找到测试数据文件? - How to make python unit tests always find test data files when run from different working directories? 如何为 bin 目录中的脚本编写 Python 单元测试 - How do I write Python unit tests for scripts in my bin directory 如何在python单元测试中模拟连接错误并请求超时 - How do I simulate connection errors and request timeouts in python unit tests 如何在Python unittest框架中简洁的实现多个相似的单元测试? - How do I concisely implement multiple similar unit tests in the Python unittest framework? 在Python中,如何编写可以访问私有属性而不暴露它们的单元测试? - In Python, how do I write unit tests that can access private attributes without exposing them? 在Python中,如何以编程方式执行存储在字符串中的单元测试? - In Python, how do you programmatically execute unit tests stored in a string? 如何在 Python 中生成动态(参数化)单元测试? - How do you generate dynamic (parameterized) unit tests in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM