从Python类/对象列表动态调用方法

2024-09-27 23:23:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有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编写代码,所以我甚至不确定要搜索什么技术或关键字。在


Tags: the方法testforreturndefsome基类
3条回答

使用修饰符来枚举类,并使用列表理解来执行方法。在

这是一个元答案-这意味着我认为你应该考虑你的设计调用测试。在

在python中,编写测试有很多成熟的方法。[1] 还有一些工具可以收集所有可用的测试并执行它们(包括统计信息、覆盖率、xml输出等等)。[2]

如果我是你的话,我会看看它们的。如果你能使用它们,就没有必要再发明轮子了。在

[1] http://docs.python.org/library/unittest.html
[2] http://readthedocs.org/docs/nose/en/latest/

我将尝试这样做:

1)将抽象类保存在test_case.py

class TestCase:
    def executeTest():
        #do some logic for the test, then return if it passed or failed

2)将所有子类保存在test_case_children.py

^{pr2}$

3)将主函数保存在main.py

from test_case import TestCase
import test_case_children

def main():
    #grab the all the elements in the script 'test_case_children'
    items = test_case_children.__dict__

    #build list of all 'TestCase' sub-classes
    test_classes = [] 
    for (key, value) in items.items():
        try:
            # check whether the item is a sub-class of 'TestCase' class
            if TestCase.__subclasscheck__(value):
                test_classes.append(value)
        except TypeError: #if item is not of type 'TestCase', ignore it
            pass

    #run the tests
    for test_class in test_classes:
        test_runner = test_class()
        test_runner.executeTest()


# this will run main() method, only when script is directly executed 
# from shell or command prompt ...
if __name__ == "__main__":
    main()

4)执行main.py脚本:

$ python main.py

注意:还有一件事,保存这些文件的文件夹还应该包含一个空的__init__.py文件,使该文件夹成为python应用程序(类似于Java中的packages或{}中的namespaces)。如果不这样做,那么这些导入语句可能就不起作用了。在

[从不同文件运行测试用例的更新]

1)档案按以下层级保存:

<root>/
   >test_case/
        >__init__.py
        >main.py
        >test_case.py
        >test_case_children/
                ->__init__.py
                ->test_case_1.py
                ->test_case_2.py
                ->test_case_3.py

2)将抽象类保存在test_case/test_case.py

class TestCase:
    def executeTest():
        #do some logic for the test, then return if it passed or failed

3)保存子类如下:

文件:test_case/test_case_children/test_case_1.py

from test_case.test_case import TestCase
class Test_Case_1(TestCase):
    def executeTest():
        #overriden function

文件:test_case/test_case_children/test_case_2.py

from test_case.test_case import TestCase
class Test_Case_2(TestCase):
    def executeTest():
        #overriden function

文件:test_case/test_case_children/test_case_3.py

from test_case.test_case import TestCase
class Test_Case_3(TestCase):
    def executeTest():
        #overriden function

4)将主函数保存在main.py

from test_case import TestCase
from test_case import test_case_children

def main():
    #grab the all the elements in the module 'test_case_children'
    items = test_case_children.__dict__

    #build list of all 'TestCase' sub-classes
    test_classes = []
    for (dict_key, dict_value) in items:
        #check whether the type of item's value is a module,
        # if its a module it's likely to contain a TestCase subclass...
        if str(type(dict_value)) == "<type 'module'>":
            for (key, value) in dict_value.items():
                try:
                    # check whether the item is a sub-class of 'TestCase' class
                    if TestCase.__subclasscheck__(value):
                        test_classes.append(value)
                except TypeError: #if item is not of type 'TestCase', ignore it
                    pass

    #run the tests
    for test_class in test_classes:
        test_runner = test_class()
        test_runner.executeTest()


# this will run main() method, only when script is directly executed 
# from shell or command prompt ...
if __name__ == "__main__":
    main()

5)执行main.py脚本:

$ cd test_case/
$ python main.py

我希望这对你有用。在

相关问题 更多 >

    热门问题