class Run_case(): # class执行前执行,且一个class只执行一次 # @classmethod # def setupClass(cls): # print('class执行前执行') # # 每个方法执行前执行一次 # def setUp(self): # print('test前执行') def __init__(self, excelpath=None, jsonpath=None): self.tablename = Case_Table().get_tablename() if excelpath: self.excelpath = excelpath else: self.excelpath = "../Test_Case/" + self.tablename + ".xls" if jsonpath: self.jsonpath = jsonpath else: self.jsonpath = "../data/" + self.tablename + ".json" self.run_method = inter() self.data = Getdata(self.excelpath, self.jsonpath) self.result = isequal() self.sendmail = sendmail() self.page_check = PageCheck() self.de_data = dependata(self.excelpath, self.jsonpath) self.caserun = Case(excelpath=self.excelpath, jsonpath=self.jsonpath) self.opexcel = oper_excel(filename=excelpath) self.DBCASE = Get_case() # 学生登陆cookies def student_cookies(self): cookies = Get_cookies(2) return cookies.getcookies_web() #admin登陆cookies def admin_cookies(self): cookies = Get_cookies(1) return cookies.get_cookies_admin() # 教师登陆cookies def teacher_cookies(self): cookies = Get_cookies(3) return cookies.getcookies_web() def go_run(self): # pass_count = [] # fail_count = [] res = None # 数据库中获取案例,写至表格中 self.DBCASE.create_excel() # 获取需执行案例总数量 rows_count = self.data.get_case_lines() # book = xlrd.open_workbook(filename=self.excelpath, formatting_info=True).sheet_by_index(0) # # 将上次的执行结果清除 # for i in range(1, rows_count): # self.opexcel.write_value(i, 13, "") # self.opexcel.write_value(i, 16, "") # del book # print("清除原测试结果数据") # 排除表头,从-1开始 for i in range(1, rows_count): caseid = self.data.get_case_id(i) url = self.data.get_url(i) functionname = self.data.get_functionname(i) web_admin = self.data.get_web_admin(i) method = self.data.get_method(i) isrun = self.data.get_is_run(i) data = self.data.get_data_json(i) file = self.data.get_files(i) return_value = self.data.get_return_value(i) # 若url为拼接方式 if "+" in url: # 则获取到拼接参数 url_data = self.de_data.depend_data(i) # print("表格里数据",url,url_data) url = url.split("+")[0] + str(url_data) # print("拼接后",url,type(url_data),type(str(url_data))) if file: files = {"logo1": ("aa.jpg", open(file, "rb"), "image/jpeg")} else: files = None # cookies = self.data.get_cookies(i) expect = self.data.get_expect(i) webexpect = self.data.get_web_expectvalu(i) depen_case = self.data.get_case_depen(i) depend_data = self.de_data.depend_data(i) # print("*****第",i,"个",data,depen_data,depen_filed) # 查看运行状态是否运行 # 判断是否是后台案例 if isrun and web_admin == 0: # 首先获取cookie,请求代入cookies cookies = self.admin_cookies() # 执行案例 self.caserun.case_run(caseid, rows_count, depen_case, depend_data, url, method, data, cookies, expect, i, webexpect, functionname, files, return_value) # 判断是否是学生端案例 elif isrun and web_admin == 1: cookies = self.student_cookies() self.caserun.case_run(caseid, rows_count, depen_case, depend_data, url, method, data, cookies, expect, i, webexpect, functionname, files, return_value) # 判断是否是教师端案例 elif isrun and web_admin == 2: cookies = self.teacher_cookies() self.caserun.case_run(caseid, rows_count, depen_case, depend_data, url, method, data, cookies, expect, i, webexpect, functionname, files, return_value)
class dependata: def __init__(self,excelpath= None,jsonpath=None): if excelpath: self.excelpath = excelpath else: self.excelpath = '../Test_Case/TestCase.xls' if jsonpath: self.jsonpath = jsonpath else: self.jsonpath='..//data//Tc_alldata.json' self.get = Getdata(self.excelpath,self.jsonpath) self.operation_excel = oper_excel(self.excelpath) def depend_data(self,i): # 判断依赖数据值的获取方式:select?depend[]?请求数据? col = int(unittesttool.Data_config.get_data_depend()) data = self.operation_excel.get_cell_value(i, col) # 获取依赖数据的值以及其所属字段 depen_data = self.get.get_data_depen(i) depen_filed = self.get.get_filed_depen(i) # 获取请求参数 request_data = self.get.get_data_json(i) # 若是查询数据库,则直接取出列表中的字典与请求参数合并 if data.startswith("select") or data.startswith("SELECT") or data.startswith("Select"): # 若是所属字段为No,则不需要合并,反之为None或有值则需要合并 try: if depen_filed == "No": return list(depen_data[0].values())[0] # 若存在数请求数据,则将请求数据与依赖数据合并作为请求数据发送 for i in range(len(depen_data)): # 若请求参数不是空,则合并 if request_data is not None: # return dict(data.items()+depen_data[0].items()) # return dict(request_data, **depen_data[0]) data_combin = request_data.copy() data_combin.update(depen_data[i]) return data_combin # 反之,则直接返回依赖数据作为请求参数 return depen_data[0] except Exception as e: print("请确认数据库查询依赖值是否存在,depen_data=",depen_data,"request_data=",request_data) elif data.startswith("depend["): #若是从depend表格中获取,则将依赖数据的值与所属字段相合并组成字典,且与请求参数合并 #拆分依赖数据值,与所属字段相结合 depend_split =list(depen_data.values())[0] depend_dict = {} depend_dict[depen_filed] = depend_split if depen_filed == "No": return list(depend_dict.values())[0] # print("组合后所属字段",depend_dict) # 若请求参数不是空,则合并 if request_data is not None: data_combin = request_data.copy() data_combin.update(depend_dict) return data_combin # 反之,则直接返回依赖数据作为请求参数 return depend_dict else: # 若是从直接给出值,则直接进行两次合并即可 depend_dict = {} depend_dict[depen_filed] = depen_data # print("oper+depen**",request_data) if request_data is not None: data_combin = request_data.copy() data_combin.update(depend_dict) return data_combin # 反之,则直接返回依赖数据作为请求参数 return depend_dict