我有X个python类,它们都继承自同一个抽象基类,它们期望子类实现一个名为executeTest()的方法
Class TestCase:
def executeTest():
#do some logic for the test, then return if it passed or failed
在我的主程序中,我需要一次加载一个继承这个基类的每个类的实例,调用executeTest(),并将结果记录到某种集合中以供以后使用。随着人们想出新的测试来编写,实现TestCase的类的数量将继续增长。在
如何在python中有效地实现这一点?我是否需要有一个单独的XML或类似类型的文件,其中包含所有单独类名的列表,然后在for循环中使用某种类加载函数?这是我第一次用python编写代码,所以我甚至不确定要搜索什么技术或关键字。在
使用修饰符来枚举类,并使用列表理解来执行方法。在
这是一个元答案-这意味着我认为你应该考虑你的设计调用测试。在
在python中,编写测试有很多成熟的方法。[1] 还有一些工具可以收集所有可用的测试并执行它们(包括统计信息、覆盖率、xml输出等等)。[2]
如果我是你的话,我会看看它们的。如果你能使用它们,就没有必要再发明轮子了。在
我将尝试这样做:
1)将抽象类保存在
test_case.py
2)将所有子类保存在
^{pr2}$test_case_children.py
3)将主函数保存在
main.py
:4)执行
main.py
脚本:$ python main.py
注意:还有一件事,保存这些文件的文件夹还应该包含一个空的}中的
__init__.py
文件,使该文件夹成为python应用程序(类似于Java
中的packages
或{namespaces
)。如果不这样做,那么这些导入语句可能就不起作用了。在[从不同文件运行测试用例的更新]
1)档案按以下层级保存:
2)将抽象类保存在
test_case/test_case.py
3)保存子类如下:
文件:
test_case/test_case_children/test_case_1.py
文件:
test_case/test_case_children/test_case_2.py
文件:
test_case/test_case_children/test_case_3.py
4)将主函数保存在
main.py
:5)执行
main.py
脚本:我希望这对你有用。在
相关问题 更多 >
编程相关推荐