dir 可以获取模块的类,方法,变量等属性!!!,通过遍历模块类来反射出类方法(getattr)

dir 可以获取模块的类,方法,变量等属性!!!

 

class Loader(object):
def __init__(self):
self.cases = {}

def load(self, path):
print("path is %s " % path)
module = importlib.import_module(path)
print("module is %s " % module)
print("dir module is %s " % dir(module))
for test_class_name in dir(module):
print("test_classname is %s " %str(test_class_name))
test_class = getattr(module, test_class_name)
print("test_class is %s " %str(test_class))
if (
isinstance(test_class, type) and
issubclass(test_class, TestCase)
):
print("mingzhong ! %s " % test_class_name)
self.cases.update({
test_class: self.find_test_method(test_class) or []
})

def find_test_method(self, test_class):
test_methods = []

for method in dir(test_class):
if method.startswith("test_"):
test_methods.append(
getattr(test_class, method)
)

return test_methods

def __iter__(self):
for test_class, test_cases in self.cases.items():
yield test_class, test_cases

posted @ 2020-08-10 10:24  峡谷恶霸  阅读(208)  评论(0编辑  收藏  举报