testrun模块呢就是最终自动化测试入口,调用前面封装的各个模块主要流程是: 1. 获取测试集种待执行的测试用例 2. 处理测试用例获取的数据,包括转换数据格式,处理数据的中的关联
testrun模块呢就是最终自动化测试入口,调用前面封装的各个模块主要流程是:
1. 获取测试集种待执行的测试用例
2. 处理测试用例获取的数据,包括转换数据格式,处理数据的中的关联等
3. 处理完数据后发生request,获取返回结果,同时处理返回结果
4. 处理返回结果包括断言结果列表,提取变量,获取测试报告所需内容等
代码如下:
from ProVar.ProjConfigVar import * from Util.getTestSuit import * from Action.KeyAction import * from Util.TestDataHandler import * from Util.AssertResult import * from Util.html_report import * from Util.ClearData import * import os import re #获取测试数据文件的绝对路径 #获取待测接口测试集 # 执行前先清除上次执行结果 clear() # 开始运行 test_suit = get_test_case_sheet_names(ExceldirPath) test_results_for_html_report = [] # 存储全局变量 global_vars = {} #实例化一个Excel实例,用于回写每一个test_case的result到Excel excel_instance_for_result_write = Excel(ExceldirPath) for test_sheet_name in test_suit: flag = True # 请求接口sheet apiName = test_sheet_name[1] excel_instance_for_result_write.set_sheet_by_name(apiName) # 获取测试步骤测试用例集 test_cases = test_cases_from_test_data_sheet(ExceldirPath,apiName) # 转换数据格式 # test_cases = pre_data_hander(test_cases,global_vars) for test_case in test_cases: test_case = pre_data_hander(test_case,global_vars) start_time = time.time() test_case_serials,method,baseurl,header,auth,body,assert_content,var_get_reg,assert_word,test_case_name= test_case # print(var_get_reg) response= api_request(method,baseurl,header,auth,body,test_case_name) end_time=time.time() try: info("接口的响应结果是:%s" %response.text) # excel书写response的状态码和response内容 excel_instance_for_result_write.write_cell_value(int(test_case[0]+1),Test_Case_executeResult_col_no+1,response.status_code) excel_instance_for_result_write.write_cell_value(int(test_case[0]+1),Test_Case_result_content_col_no+1,response.text) # excel书写测试执行时间 excel_instance_for_result_write.write_current_time(int(test_case[0]+1),Test_Case_execute_time_col_no+1) if assert_content:#如果有断言sheet info("断言表为:%s" % assert_content) if assert_result(excel_instance_for_result_write,response,assert_content): # excel书写断言结果 excel_instance_for_result_write.write_cell_value(int(test_case[0] + 1),Test_Case_assert_result_col_no + 1, "成功", "green",apiName) # 写report需要的字段 test_results_for_html_report.append( (test_case_name,response.url, body, response.text, int((end_time - start_time) * 1000), assert_content, "成功")) else: excel_instance_for_result_write.write_cell_value(int(test_case[0] + 1), Test_Case_assert_result_col_no + 1, "失败", "red",apiName) # 写report需要的字段 test_results_for_html_report.append( (test_case_name,response.url, body, response.text, int((end_time - start_time) * 1000), assert_content, "失败")) flag = False # 如果仅仅是单纯的验证某一个返回结果字段 elif assert_word: info("断言字段为:%s" % assert_word) if re.search(assert_word,response.text): # excel书写断言结果 excel_instance_for_result_write.write_cell_value(int(test_case[0] + 1),Test_Case_assert_result_col_no + 1, "成功", "green",apiName) # 写report需要的字段 test_results_for_html_report.append( (test_case_name,response.url, body, response.text, int((end_time - start_time) * 1000), assert_content, "成功")) else: excel_instance_for_result_write.write_cell_value(int(test_case[0] + 1), Test_Case_assert_result_col_no + 1, "失败", "red",apiName) # 写report需要的字段 test_results_for_html_report.append( (test_case_name,response.url, body, response.text, int((end_time - start_time) * 1000), assert_content, "失败")) flag = False #如果var_get_reg不为空,即要求提取返回变量 if var_get_reg: var_name = var_get_reg.split("||")[0] regx = var_get_reg.split("||")[1] # print(var_name,regx) if re.findall(regx, response.text): var_value = re.findall(regx, response.text) # 去除重复数据 var_value = list(set(var_value)) exec("global_vars[‘%s‘]=%s" % (var_name, var_value)) info("从响应中提取的变量名%s,变量值为%s" % (var_name, var_value)) info("生成全局变量名: global_vars[‘%s‘]=%s" % (var_name, var_value)) # 将提取的变量写入excel excel_instance_for_result_write.write_cell_value(int(test_case[0] + 1),Test_Case_var_result_col_no+1,str(var_value),None,apiName) except IndexError: info(‘提取变量失败:‘+traceback.format_exc()) excel_instance_for_result_write.write_cell_value(int(test_case[0] + 1),Test_Case_var_result_col_no+1,"Failed","red",apiName) except: # traceback.print_exc() excel_instance_for_result_write.write_cell_value(int(test_case[0] + 1), Test_Case_executeResult_col_no+1, "失败","red",apiName) if not response: error("请求接口系统无法连接") # break test_results_for_html_report.append( (test_case_name,baseurl, body, response, int((end_time - start_time) * 1000), assert_content, "失败")) else: test_results_for_html_report.append( (test_case_name,response.url, body, response.text, int((end_time - start_time) * 1000), assert_content, "失败")) flag = False info("接口请求的耗时为%d 毫秒" %((end_time - start_time)*1000)) excel_instance_for_result_write.set_sheet_by_index(1) if flag: excel_instance_for_result_write.write_cell_value(int(test_sheet_name[0]) + 1, Test_Suit_execute_result_col_no+1, "成功","green") else: excel_instance_for_result_write.write_cell_value(int(test_sheet_name[0]) + 1, Test_Suit_execute_result_col_no+1, "失败","red") excel_instance_for_result_write.write_current_time(int(test_sheet_name[0]) + 1, Test_Suit_execute_time_col_no+1) # print(global_vars) html_name = ‘接口测试报告‘ report_html(test_results_for_html_report, html_name)