class DependentData: def __init__(self, case_id): self.case_id = case_id self.opera_excel = OperateExcel() self.data = GetData() # 通过case_id去获取该case_id的整行数据 def get_case_line_data(self): row_data = self.opera_excel.get_rows_num(self.case_id) return row_data # 执行依赖测试,获取结果 def run_dependent(self): run_method = RunMethod() row = self.opera_excel.get_rows_num(self.case_id) request_data = self.data.get_data_for_json(row) # self.get_case_line_data(case_id) header = self.data.get_header(row) #headers = json.dumps(header) method = self.data.get_request_method(row) url = self.data.get_url(row) data = self.data.get_data_for_json(row) res = RunMethod.run_main(method, url, data, header) return json.loads(res) # 根据依赖的key去获取执行依赖测试case的响应,然后返回 def get_data_for_key(self, row): depend_data = self.data.get_depend_key(row) response_data = self.run_dependent() json_exe = parse(depend_data) madle = json_exe.find(response_data) return [math.value for math in madle[0]]
class DependentData: def __init__(self,case_id): self.case_id=case_id self.opera_excle=OperationExcle() self.data=GetData() #通过case_id去获取该case_id的整行数据 def get_case_line_data(self): rows_data=self.opera_excle.get_rows_data(self.case_id) return rows_data #执行依赖测试,获取结果 def run_dependent(self): run_method=RunMethod() row_num=self.opera_excle.get_row_num(self.case_id) # request_data=self.data.get_data_for_json(row_num) request_data = self.data.get_request_data(row_num) header=self.data.get_is_header(row_num) method=self.data.get_request_method(row_num) url=self.data.get_url(row_num) res=run_method.run_main(method,url,request_data,header) return json.loads(res) #根据依赖key去获取执行依赖测试case的响应,然后返回 def get_data_for_key(self,row): depend_data=self.get_data_for_key(row) #response_data为依赖测试的执行结果 response_data=self.run_dependent() #定义要获取的key json_exe=parse(depend_data) #定义响应数据,key从响应数据里获取 madle=json_exe.find(response_data) #math.value返回的是一个list,可以使用索引访问特定的值jsonpath_rw的作用就相当于从json里面提取响应的字段值 return [math.value for math in madle][0]
class Run_test: def __init__(self): self.data = GetData() self.runMethod = RunMethod() def run_case(self): rows = self.data.get_case_lines() #row = 0 for row in range(1, rows): is_run = self.data.get_is_run(row) print(is_run) if is_run == True: url = self.data.get_url(row) print(url) method = self.data.get_request_method(row) print(method) #header = self.data.get_header(row) depend_case = self.data.is_depend(row) print(depend_case) #request_data = self.data.get_data_for_json(row) request_data = self.data.get_request_data(row) body = json.dumps(request_data) print(request_data) #method = 'post' #url = 'http://10.128.0.150:8002/backend/user/login' headers = {"Content-Type": "application/json;charset=UTF-8"} #headers = json.dumps(headerstring) '''bodystring = {"username" : "super", "password" : "Connext@0101"} body = json.dumps(bodystring)''' #res = self.runMethod.run_main(method, url, request_data, headers) print("==========") #print(res) if depend_case != None: depend_data = DependentData(depend_case) depend_res = depend_data.run_dependent(depend_data.case_id) print(depend_res) #self.depend_data = DependentData(depend_case) # 获取依赖响应数据 depend_response_key = depend_data.get_data_for_key(row) # 获取依赖的key(数据依赖字段excel) depend_key = self.data.get_depend_field(row) print(depend_key) #request_data[depend_key] = depend_response_key #op_json = OperrationJson('../dataconfig/user.json') # header = OperrationJson.get_data('token') #headers = {'Content-Type': 'application/json;charset=UTF-8'} #body = {'username': '******', 'password': '******'} else: res = self.runMethod.run_main(method, url, request_data, headers) print(res) else: print("NO way")
class login(): def __init__(self): self.Opexl = OperationExcle('../dataCase/login.xls', 0) self.data = GetData() self.com_util = CommonUtil() self.run_method = RunMethod() def test_Login(self): rownums = self.data.get_case_line() for i in range(1, rownums): is_run = self.data.get_is_run(i) # print(is_run) url = self.data.get_url(i) # print(url) method = self.data.get_request_method(i) request_data = self.data.request_data_type_change(i) # print(request_data) expect = self.data.get_expect_data(i) # print(expect) header = self.data.is_header(i) select_str = 'select' insert_str = 'INSERT' update_str = 'UPDATE' delete_str = 'DELETE' for except_num in select_str, insert_str, update_str, delete_str: except_str = self.com_util.is_contain(except_num, expect) if except_str is True: expect = self.data.get_sql_expect_data(i) else: expect = self.data.get_expect_data(i) # print(expect) if is_run is True: res = self.run_method.run_main(method, url, request_data, header) if except_str is False: if self.com_util.is_contain(expect, res) == True: self.data.write_result(i, 'pass') print("测试通过") else: self.data.write_result(i, 'Filed') print('测试失败') # print(res) if except_str is True: if self.com_util.is_equal_dict(expect, res) == True: # 判断字典是否相等 self.data.write_result(i, 'pass') print('测试通过') else: self.data.write_result(i, res) print('测试失败') print(res) else: pass
class RunTest: def __init__(self): self.run_method = RunMethod() self.data = GetData() self.com_util = CommonUtil() self.send_mail = SendEmail() self.op_testReport = Write_testReport_excle() # 程序执行 def go_on_run(self): global pass_count, fail_count res = None pass_count = [] fail_count = [] rows_count = self.data.get_case_line() # print(rows_count) for i in range(1, rows_count): is_run = self.data.get_is_run(i) # print(is_run) url = self.data.get_url(i) # print(url) method = self.data.get_request_method(i) request_data = self.data.request_data_type_change(i) # print(request_data) expect = self.data.get_expect_data(i) # print(expect) header = self.data.is_header(i) select_str = 'select' insert_str = 'INSERT' update_str = 'UPDATE' delete_str = 'DELETE' for except_num in select_str, insert_str, update_str, delete_str: except_str = self.com_util.is_contain(except_num, expect) if except_str is True: expect = self.data.get_sql_expect_data(i) else: expect = self.data.get_expect_data(i) # print(expect) errorNum = 0 if is_run is True: while errorNum <= Config_Try_Num: # 判段失败次数是否小于等于配置失败重试次数 res = self.run_method.run_main(method, url, request_data, header) if except_str is False: if self.com_util.is_contain(expect, res) == True: self.data.write_result(i, 'pass') print("测试通过") pass_count.append(i) errorNum = 0 break else: self.data.write_result(i, 'Filed') # print('测试失败,重试中') if errorNum < Config_Try_Num: errorNum += 1 print("测试失败,重试中,当前重试次数为第%s次" % (errorNum)) else: print("重试次数已用完,测试失败") fail_count.append(i) break # print(res) if except_str is True: if self.com_util.is_equal_dict( expect, res) == True: # 判断字典是否相等 self.data.write_result(i, 'pass') pass_count.append(i) print('测试通过') else: self.data.write_result(i, 'Filed') # print('测试失败,重试中') if errorNum < Config_Try_Num: errorNum += 1 print("测试失败,重试中,当前重试次数为第%s次" % (errorNum)) else: print("重试次数已用完,测试失败") fail_count.append(i) break else: pass print(res) # print(self.com_util.is_contain(expect,res)==True) # 发送邮件、生成测试报告 def create_test_report(self): self.op_testReport.write_TestReport(pass_count, fail_count) # 生成excel表格测试报告 self.op_testReport.excle_to_html() # 将测试报告转换为html输出 self.send_mail.send_main(pass_count, fail_count) # 发送测试报告邮件