class Run(object): def __init__(self): self.common = Common() self.my_log = MyLog() self.send_email = SendEmail() self.cases = ProduceCases() self.cases.produce_case() # 自动生成接口测试用例 self.path = self.common.get_result_path() # 获取报告存储路径 self.log = self.my_log.get_log().logger # log日志 self.suit = unittest.TestSuite() # 测试套件(定义执行顺序) # 方式一: def add_api_test(self): """添加api测试用例""" from NT.cases.api.test_cases import APITestCases # 生成所有接口测试用例后导入TestCases类 for origin, sheet_dict in self.common.api_cases_dict.items(): for case_name, case_params in sheet_dict.items(): self.suit.addTest(APITestCases("test_%s" % case_name)) # 方式一: def add_ui_test(self): """添加ui测试用例""" self.suit.addTest(WebLogin("test_web_login")) # web登录 self.suit.addTest(AppLogin("test_app_login")) # app登录 self.suit.addTest(AppOperation("test_app_operation")) # 技术操作考核 self.suit.addTest(AppExams("test_app_exams")) # 考试 self.suit.addTest(AppPractice("test_app_practice")) # 练习 self.suit.addTest(AppSurvey("test_app_survey")) # 问卷调查 self.suit.addTest(AppCurriculum("test_app_curriculum")) # 课程学习 self.suit.addTest(AppCourseware("test_app_courseware")) # 课件学习 # 方式二: def add_cases(self): """添加所有测试用例""" cases_path = self.common.get_path("cases") # 用例路径 cases_file = "test*.py" # 用例文件或用例模式 discover = unittest.defaultTestLoader.discover(cases_path, pattern=cases_file, top_level_dir=None) return discover # 方式二: def run_cases(self, case): """执行用例并生成报告""" result = BeautifulReport(case) result.report(log_path=self.path, filename="NT_测试报告.html", description='NT自动化测试')
def __init__(self): try: self.config_path = Common.get_path("data", "config.ini") # 拼接配置文件路径 self.cf = configparser.ConfigParser() # 读取配置文件的对象 # 判断文件是否存在 if not os.path.isfile(self.config_path): raise Exception("文件%s不存在!" % self.config_path) # 判断是否有BOM头 bom = b'\xef\xbb\xbf' # BOM头多出的内容 judge_bom = lambda s: True if s == bom else False # 定义一个匿名函数 with open(self.config_path, 'rb') as fr: if judge_bom(fr.read(3)): # 读取头3个字节进行判断 data = fr.read() with open(self.config_path, 'wb') as fw: fw.write(data) # 利用二进制重新写入后BOM头就消失了 # 读取文件 self.cf.read(self.config_path, encoding="utf-8") except Exception as e: raise Exception("ReadConfig.__init__异常 %s" % e)
def produce_case(self): """生成所有api用例""" try: self.log.debug("api用例路径:%s" % self.api_cases_path) # self.log.debug(self.common.api_cases_dict) base_case_path = Common.get_path( "cases", "api", "base_case.py") # 拼接用例解析函数模板(base_case.py)路径 test_cases_path = Common.get_path( "cases", "api", "test_cases.py") # 拼接存放生成的所有测试用例的文件路径 with open(base_case_path, "r", encoding="utf-8") as file_old: # 从file_old中读取 with open(test_cases_path, "w", encoding="utf-8") as file_new: # 写入file_new中 lines = file_old.readlines() # 按行读取file_old中的所有数据 # 不需要改变的部分 i = 1 # 不需要改变的行号 for line in lines: if "# 定位标记" in line: break file_new.write(line) i += 1 # 需要改变的部分 n = 0 # 用例编号 global case_name # 用例名 for origin, sheet_dict in self.api_cases_dict.items(): # key:origin(项目地址原点),value:sheet_dict(单个sheet中的用例集合) for case_name, case_params in sheet_dict.items( ): # key:case_name(用例名),value:case_params(一条用例) n += 1 j = i # 需要改变的行号 while j < len(lines): # 动态生成一个用例 if "def test_case(self):" in lines[j]: line = lines[j].replace( "test_case", "test_%s" % case_name) elif "用例描述" in lines[j]: line = lines[j].replace( "用例描述", str(case_params["remark"])) elif "case_params = {}" in lines[j]: line = lines[j].replace( "{}", str(case_params)) elif "origin = 'null'" in lines[j]: line = lines[j].replace("null", origin) elif "case_name = 'null'" in lines[j]: line = lines[j].replace("null", case_name) elif "case_num = 0" in lines[j]: line = lines[j].replace("0", str(n)) elif "self.execute_case" in lines[j]: line = lines[j] + "\n" else: line = lines[j] # 不在上边的其他行(空行) file_new.write(line) j += 1 self.log.debug("api用例数量:%s" % n) self.log.debug("*" * 100 + "\n") except Exception as e: self.log.error(e) raise Exception("请检测用例%s格式是否正确!" % case_name)