class TokenCheck(): TOKEN_NOT_OK_VALUE = "Token parsing exception" TOKEN_NOT_OK_VALUE2 = "请求token失效" COOKIE_NOT_OK_VALUE = "" SUCCESS_LOGIN_CASE_ID = "login_01" def __init__(self): self.depent_data = DependentData() self.comtool = CommonUtil() ##适配 .响应body 里包含了 失效的文字,则重新获取token def check_token_exception(self, response_body): if self.comtool.is_contain(TokenCheck.TOKEN_NOT_OK_VALUE,str(response_body) )\ or self.comtool.is_contain( TokenCheck.TOKEN_NOT_OK_VALUE2,str(response_body)) : print("token失效了,重新获取") if self.__reget_token(): print("重新获取token成功") return True return False ## return False def __reget_token(self, loginid=None): if not loginid: loginid = TokenCheck.SUCCESS_LOGIN_CASE_ID #重新执行,获取token接口 ret = self.depent_data.force_runcase_by_caseid(loginid, token=True) return ret
class RunTest: def __init__(self): self.run_method = RunMethod() self.data = GetSheetData() self.common_util = CommonUtil() def go_run(self): rows_count = self.data.get_case_lines() for i in range(1, rows_count): is_run = self.data.is_run(i) id = self.data.get_id(i) url = self.data.get_url(i) method = self.data.get_method(i) header = self.data.get_header(i) depend_id = self.data.get_depend_id(i) depend_data = self.data.get_depend_data(i) depend_key = self.data.get_depend_key(i) data = self.data.get_request_data(i) expect = self.data.get_expect(i) if is_run: res = self.run_method.main(method, url, header, data) # 把结果写入excel # print(expect) # print(res) if self.common_util.is_contain(expect, res): self.data.write_result(i, 'pass') print(u"测试通过") else: self.data.write_result(i, res) print(u"测试失败") return res
class RunTest: def __init__(self): self.run_method = RunMethod() self.data = GetData() self.com_util=CommonUtil() def go_on_run(self): # 获取case的行数 rows_count = self.data.get_case_lines() print(rows_count) for i in range(1,rows_count): # 获取需要的列的内容 is_run=self.data.get_is_run(i) if is_run: url=self.data.get_request_url(i) method=self.data.get_request_method(i) request_data=self.data.get_data_for_json(i) expect=self.data.get_expcet_data(i) header=self.data.is_header(i) depend_case=self.data.is_depend(i) if depend_case!=None: self.depend_data=DependdentData(depend_case) # 获取依赖响应的数据【依赖case里面的数据】 depend_response_data=self.depend_data.get_data_for_key(i) # 获取依赖的key depend_key=self.data.get_depend_field(i) # 更新请求数据里面的内容 request_data[depend_key]=depend_response_data res=self.run_method.run_main(method,url,request_data,header) print(res) if self.com_util.is_contain(expect,res): print("测试通过") self.data.write_result(i,"pass") else: print("测试失败") self.data.write_result(i,res)
class DependentData(): def __init__(self): # self.case_id = case_id self.get_data = GetData() self.run_method = RunMethod() self.com_util = CommonUtil() self.opera_excel = OperationExcel() def _get_case_row_num_by_caseid(self): rownum = self.get_data.get_case_row_by_idname(self.case_id) return rownum # rownum = self.get_data.get_case_row_by_idname(self.case_id) ## token=True,或者cookie=True是为了 refresh token使用的。单独执行一条case def _run_dependent_case(self, token=None, cookies=None): # 不需要 isrun # i = self.get_data.get_case_row_by_idname(self.case_id) #获取id的行号 i = self._get_case_row_num_by_caseid() method = self.get_data.get_request_method(i) url = self.get_data.get_url_final(i) data = self.get_data.get_request_data_final(i) header = self.get_data.get_header_info(i) expect_result = self.get_data.get_expect_result_final(i) # 执行请求 , 获得的结果,list[0]为响应code, [1]为结果 if token: run_response_data = self.run_method.run_main(method, url, data=data, header=header) # print("#$$# :",str(run_response_data)) op_token = OperationToken() token_value = op_token.trans_response_token_value_by_body(run_response_data[2]) op_token.write_token(token_value) return token_value elif cookies: run_response_data = self.run_method.run_main(method, url, data=data, header=header) op_cookie = OperationCookie() cookie_value = op_cookie.trans_response_cookie_value(run_response_data[2]) op_cookie.write_cookie(cookie_value) return cookie_value else: is_dependent = self.get_data.get_is_dependent(i) if is_dependent: op = DependentData() rs = op.get_dependent_data_for_key(i) # print('run dp data get : ' + rs) if not rs: # 如果返回False, 就是依赖的case 失败了,那么这个例子也不用跑了 print('fail , dependent return case fail') self.get_data.write_test_result(i, 'fail, dependent case return fail') return False # print('run dp data get : ' + rs) filed_data = self.get_data.get_dependent_filed(i) data[filed_data] = rs run_response_data = self.run_method.run_main(method, url, data, header) try: if self.com_util.is_contain(expect_result, run_response_data[1]): return json.loads(run_response_data[1]) else: return False except Exception as e: print(e) # 通过 dependent 的key获取真实的 value内容 def get_dependent_data_for_key(self, row): self.case_id = self.get_data.get_dependent_caseid(row) # 获取case id denpendet_data = self.get_data.get_dependent_data(row) # 获取 依赖上层的data key print('dp data:' + denpendet_data) response_data = self._run_dependent_case() # 运行依赖 print('rs data : ', response_data) if not response_data: return False json_exe = parse(denpendet_data) madle = json_exe.find(response_data) arr0 = [math.value for math in madle][0] return arr0 # 获取 依赖data ##如果想重新强制获取token更新,则提供登录的caseid:login_01, token=True def force_runcase_by_caseid(self, caseid, token=None, cookies=None): self.case_id = caseid if token: return self._run_dependent_case(token=True) # TODO if cookies: return True return self._run_dependent_case()
class RunTest(): def __init__(self): self.run_method = RunMethod() self.get_data = GetData() self.com_util = CommonUtil() self.send_mail = SendMail() self.op_cookie = OperationCookie() self.op_token = OperationToken() self.token_check = TokenCheck() self.save_body_values = SaveBodyValue() self.fail_count = [] self.break_count = [] self.pass_count = [] def _result_handler(self, row, save_body_value, expect_result, current_result, expect_code, current_code): if self.com_util.is_contain(expect_result, current_result) \ and str(expect_code) == str(current_code): # log.info('----pass') # self.pass_count.append(row) # self.get_data.write_test_result(row, 'pass') # 成功后,检查是否有需要保存的变量和需要正则获取的值(json) if save_body_value: log.debug("##get 需要保存值:" + str(save_body_value)) if not self.save_body_values.save_value_to_conf( save_body_value, current_result): log.error("保存 响应值的保存记录失败") ##判断case失败 else: return False # print('----fail') # self.fail_count.append(row) # self.get_data.write_test_result(row, 'fail') # 程序执行 def get_on_run(self): log.info('执行Sheet : [' + self.get_data.get_current_sheet_name() + ']') rows_count = self.get_data.get_case_lines() for row_num in range(2, rows_count + 1): # 因为openpyxl是从1计数 # 获取 测试的值 is_run = self.get_data.get_is_run(row_num) if not is_run: #不运行,则记录 skip case # print('Skip case : ', self.get_data.get_case_id_name(row_num)) # self.get_data.write_test_result(row_num, 'Not run,skip case') continue # 判断是否执行不执行,直接跳过 log.info('Start case : ' + self.get_data.get_case_id_name(row_num)) #获取row 测试相关信息 try: method = self.get_data.get_request_method(row_num) url = self.get_data.get_url_final(row_num) data = self.get_data.get_request_data_final(row_num) header = self.get_data.get_header_info(row_num) #获取的 header的值 is_cookie = self.get_data.get_is_cookie( row_num) #获取cookie\token相关信息 进行cookie或者token处理 expect_result = self.get_data.get_expect_result_final( row_num) #获取期望的结果(包含的内容) expect_code = self.get_data.get_expect_code( row_num) #获取期望的code # save_body_value类似 -- {"user1":"data.records[0].name","user2":"data.records[1].name"} save_body_value = self.get_data.get_save_value( row_num ) #获取需要保存的key,value.最终保存在conf.ini的valueauto section中 # 判断是否有 依赖,如果有 就进行依赖case的测试。如果没有就 直接进行 该api基本测试 # dp_caseid = self.get_data.get_dependent_caseid(row_num) is_dependent = self.get_data.get_is_dependent(row_num) except Exception as e: log.error("----Has some error : " + traceback.print_exc()) self.break_count.append(row_num) self.get_data.write_test_result(row_num, 'break' + str(e)) log.error('----break case') continue if is_dependent: op = DependentData() dependent_response_data = op.get_dependent_data_for_key( row_num) if not dependent_response_data: # 如果返回False, 就是依赖的case 失败了,那么这个例子也不用跑了 log.error('----fail , dependent return case fail') self.get_data.write_test_result( row_num, 'fail, dependent case return fail') self.fail_count.append(row_num) continue # log.error('run dp data get : ' + rs) filed_data = self.get_data.get_dependent_filed( row_num) #获取 当前case 要被替换的字段内容 data[filed_data] = dependent_response_data #处理cookie : wc更新cookies,yc携带cookies ,nc不需要cookies if is_cookie == "wc": #需要更新cookies,通过响应值获取ck run_response_data = self.run_method.run_main(method, url, data=data, header=header) op_cookie = OperationCookie() cookie_value = op_cookie.trans_response_cookie_value( run_response_data[2]) op_cookie.write_cookie(cookie_value) elif is_cookie == "yc": # self.op_cookie.get_cookie('loongaio') cookie = self.op_cookie.get_cookie_file_data() cookie = json.loads(cookie) run_response_data = self.run_method.run_main(method, url, data=data, cookie=cookie, header=header) elif is_cookie == "nc": run_response_data = self.run_method.run_main(method, url, data=data, header=header) # 处理token elif is_cookie == "wt": #需要更新token,通过响应值获取token run_response_data = self.run_method.run_main(method, url, data=data, header=header) # log.debug("#$$# :"+str(run_response_data)) op_token = OperationToken() token_value = op_token.trans_response_token_value_by_body( run_response_data[2]) op_token.write_token(token_value) elif is_cookie == "yt": token = self.op_token.get_token() log.debug("#携带的token#:" + token) header["token"] = token ##根据当前web系统适当调整,这里的token是加在header run_response_data = self.run_method.run_main(method, url, data=data, header=header) if self.token_check.check_token_exception(run_response_data): token = self.op_token.get_token() header["token"] = token ##根据当前web系统适当调整,这里的token是加在header run_response_data = self.run_method.run_main(method, url, data=data, header=header) else: #其他值,则不需要token和cookies run_response_data = self.run_method.run_main(method, url, data=data, header=header) # 执行请求 , 获得的结果,list[0]为响应code, [1]为结果 , [2] 为 response 本身。 把当前 实际结果写到excel current_code = str(run_response_data[0]) current_result = str(run_response_data[1]) self.get_data.write_current_result(row_num, current_result) self.get_data.write_current_code(row_num, current_code) try: #判断case成功 result_check_fail_flag = False #检查响应结果是否和预期结果一致 # expect_result=list(expect_result) #判断多个预期值的情况一样。数组形式的 for exp_res in expect_result: log.debug("#预期检查值#:" + exp_res) if not self.com_util.is_contain(exp_res, current_result): log.info('----fail') self.fail_count.append(row_num) self.get_data.write_test_result(row_num, 'fail') result_check_fail_flag = True log.error("响应断言不匹配 %s" % exp_res) break if not result_check_fail_flag: #响应 断言检查成功后,检查code断言 expect_result = expect_result[0] if int(expect_code) == int(current_code): log.info('----pass') self.pass_count.append(row_num) self.get_data.write_test_result(row_num, 'pass') # 成功后,检查是否有需要保存的变量和需要正则获取的值(json) if save_body_value: log.debug("##get 需要保存值" + str(save_body_value)) if not self.save_body_values.save_value_to_conf( save_body_value, current_result): log.error("保存 响应值的保存记录失败") ##判断case失败 else: log.error("code不一致%s %s" % (expect_code, current_code)) log.info('----fail') self.fail_count.append(row_num) self.get_data.write_test_result(row_num, 'fail') except Exception as e: log.error(traceback.print_exc()) self._send_test_mail() #输出结果,并发送邮件 def _fail_row_info(self): f_arr = [] for i in self.fail_count: r_info = '[' + self.get_data.get_current_sheet_name() + '] ' + 'Row: ' + str(i) + ', caseid : ' +\ self.get_data.get_case_id_name(i) + ', Url : ' + self.get_data.get_url_final(i) + \ ", Comment : " + self.get_data.get_comment_info(i) f_arr.append(r_info) return f_arr def _send_test_mail(self): passnum = len(self.pass_count) failnum = len(self.fail_count) breaknum = len(self.break_count) totalnum = passnum + failnum + breaknum result = "%.2f%%" % (passnum / totalnum * 100) fm = [i for i in self._fail_row_info()] if fm: #有失败时,打印信息 content = "这次接口运行情况如下:\n 总计运行接口个数: %s 。通过: %s , 失败: %s , 中断: %s \n 通过百分比:%s " \ "\n 失败接口如下:\n %s" % \ (totalnum,passnum,failnum,breaknum,result,'\n '.join(fm)) else: #全部成功时,打印信息 content = "这次接口运行情况如下:\n 总计运行接口个数: %s 。通过: %s , 失败: %s , 中断: %s \n 通过百分比:%s " % \ (totalnum, passnum, failnum, breaknum, result) log.info(content) sub = "自动化测试邮件-api" sm = SendMail()