Python 动态导入类并运行其中的方法

    for case in case_name:
        mod = importlib.import_module('tests.'+case)#动态导入tests目录下的case
        clas = getattr(mod, case)#实例化case
        atts = dir(clas)#获得case属性
        for att in atts:
            if 'test_' in att:
                print('%s running......' % att)
                test = getattr(clas(), att)()#运行含有‘test_’的方法
                print('%s is complete' % att)
原文地址:https://www.cnblogs.com/gexbooks/p/10918784.html