简体   繁体   English

Python:运行相同的Unittest模块测试多个文件

[英]Python: Run The Same Unittest module Tests For Multiple Files

I am attempting to create a simple framework that will discover all of the test cases from a specific directory (I am using unittest for these cases) and run each of these test cases against multiples python files that will all implement the same code with the same function signatures. 我正在尝试创建一个简单的框架,该框架将从特定目录中发现所有测试用例(我在这些情况下使用unittest),并对多个python文件运行这些测试用例,这些文件都将使用相同的代码实现相同的代码功能签名。

Autograder.py  
TestCasesFolder/
    TestCase1.py  
    TestCase2.py  
    ...  
ImplementationFolder/
    Implementation1.py
SecondImplementationFolder/
    Implementation2.py

The framework succesfully finds all of the test case using (note this is in the class) 框架成功使用查找所有测试用例(请注意,这是在类中)

self.suites = unittest.defaultTestLoader.discover(self.testDirectory)

From there, I would like to run these suites on both Implementation1 and Implementation2. 从那里,我想在Implementation1和Implementation2上运行这些套件。

I have been using the built in 我一直在使用内置

self.suites.run(unittest.TestResult)

method from unittest to run my tests, and my first attempt at solving this problem was to import the current implementation I wanted to test using unittest的方法来运行我的测试,而我解决该问题的第一个尝试是导入要使用的当前实现进行测试

imp.load_source

and then update the global namespace for the TestCase1.py with the correct module reference. 然后使用正确的模块引用更新TestCase1.py的全局名称空间。 However, because each module has its own global namespace I'm not sure if I can hook into the other files namespace. 但是,由于每个模块都有其自己的全局名称空间,所以我不确定是否可以连接到其他文件名称空间。 I am also not sure if this the correct approach, or if there is a better way than my implementation. 我也不确定这是否正确,或者是否有比我的实现更好的方法。 How should I go about doing this? 我应该怎么做呢?

EDIT My current solution that seems to work is for the Autograder.py file to update the __builtins__ module with a reference to the Implementation module. 编辑我当前似乎有效的解决方案是使Autograder.py文件通过参考Implementation模块来更新__builtins__模块。 The actual line looks like: 实际行如下所示:

__builtins__.ImplementationModule = imp.load_source("Implementation Module", "Implementation1.py")

This means when the TestCase1.py has access to ImplementationModule through __builtins__ . 这意味着当TestCase1.py通过__builtins__可以访问ImplementationModule时。 Of course the problem is this assumes that the __builtins__ module never implements anything that has the name ImplementationModule otherwise I will overwrite it with unknown implications. 当然,问题在于这是假定__builtins__模块从不实现任何名称为ImplementationModule的东西,否则我将以未知的含义覆盖它。 Is there a less risky version of doing this? 有这样做的风险较小的版本吗?

Have you looked at the nose system? 你看过鼻子系统吗? It sounds very similar to what you are doing. 听起来与您的工作非常相似。

http://readthedocs.org/docs/nose/ http://readthedocs.org/docs/nose/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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