python自动加载类的方法(测试用例使用)
2020-10-25
python
1706
import importlib import os import optparse ''' import re from app.BaiduRand import BaiduRand class TestLink: @staticmethod def test_link(): baidu = BaiduRand() link = baidu.createRandUrl(5, "{数字3}/{数字6}") # 366/943047 # 716/754962 # 498/386146 # 815/454851 # 591/220477 r = re.match("^\d{3}/\d{6}", link) if r is not None: print("验证链接格式成功100%.") assert True else: print("验证链接格式失败0%.") assert False ''' def test_module(module_name): module = importlib.import_module(module_name) module_dir = dir(module) for element in module_dir: if (isinstance(element, object) and (element.startswith('Test'))): print("加载类:%s()" % (element)) TestClass = getattr(module, element) obj = TestClass() # print(obj) func_names = dir(TestClass) for func_name in func_names: if func_name.startswith('test_'): print("执行方法:%s.%s()" % (element,func_name)) # get function func = getattr(obj, func_name) # call this function func() def main(): parse = optparse.OptionParser(usage='"usage:%prog [options] arg1,arg2"', version="%prog 1.0") parse.add_option("-f", "--file", dest="file", type=str, metavar="file", help="请输入测试用例的类") option, args = parse.parse_args() class_name = "" if option.file is None: if len(args) > 0: class_name = args[0] if class_name != "": # 单个调用 test_module("test.Test"+class_name.capitalize()) else: # 所有用例测试 path = "./test" for x in os.listdir(path): if x.startswith("Test"): file = os.path.join(path,x) module = os.path.splitext(os.path.basename(file))[0] module_class = "test."+module test_module(module_class) #test_module("test.TestLink") if __name__ == "__main__": main()
很赞哦! (0)
相关文章
文章评论
-
-
-
0条评论