def go_on_run(self): res = None pass_conut = [] fail_conut = [] rows_conut = self.data.get_case_lines() for i in range(1, rows_conut): is_run = self.data.get_is_run(i) if is_run: self.request_type = self.data.get_request_type(i) url = self.data.get_request_url(i) method = self.data.get_request_method(i) request_data = self.data.get_json_data(i) case_name = self.data.get_case_name(i) cookie = self.data.get_cookies_value(i) expect = self.data.get_expect_data(i) depent_case = self.data.case_id(i) # 获取依赖字段的key if depent_case is not None: # 获取依赖的key depent_key = self.data.get_depnet_file(i) self.depend_data = DepnedentData(depent_case) # 获取依赖的响应数据 depnet_response_data = self.depend_data.get_data_for_key(i) le = len(depent_key.split(">")) # 切分字符串取大小 for y in range(0, le): re = depent_key.split(">") request_data[re[y]] = depnet_response_data[y] # 循环替换 if cookie == 'write': # 发送请求 res = self.run_method.run_main(method, url, request_data) op_cookies = OperationHeader(res) op_cookies.write_cookie() # 写入cookies到cookie.json文件中 elif cookie == 'yes': get_cookie = OperationJson('../dataconfig/cookie.json') cooki = get_cookie.get_data('SESSION') cookies = { "SESSION": cooki } res = self.run_method.run_main(method, url, request_data, cookies) else: res = self.run_method.run_main(method, url, request_data) if self.containt.is_contain(expect, res): self.data.write_result(i, 'pass') pass_conut.append(i) self.logger.info("测试通过") self.logger.info("通过响应数据" + res) print("测试通过") else: self.logger.info("测试失败") self.logger.info("失败case名称:" + case_name) self.logger.info("失败的行数:" + str(i)) self.data.write_result(i, res) fail_conut.append(i) print("测试失败") self.logger.info("url:" + url) self.logger.info("失败请求数据:" + str(request_data)) self.logger.info("失败响应数据:" + res) # 发送测试报告邮件 self.email.send_main(pass_conut, fail_conut)
class RunTest: def __init__(self, file_name=None, sheet_id=0): self.data = GetData(file_name, sheet_id) self.oper_header = OperationHeader() self.run = RunMethod() self.comm = Common() self.send_mail = SendEmail() def go_on_run(self): lines = self.data.get_case_lines() pass_count = [] fail_count = [] for i in range(1, lines): is_run = self.data.get_is_run(i) if is_run: res = None url = self.data.get_url(i) method = self.data.get_method_type(i) fix_request_key = self.data.get_fix_request_key(i) request_data = self.data.get_request_for_json(i) expected = self.data.get_expected_result(i) header = self.data.get_header(i) is_depend = self.data.is_depend(i) if is_depend is not None: depend_case_id = self.data.get_depend_case_id(i) depend_request = self.data.get_depend_request(i) depend_data = DependData(depend_case_id) depend_response = depend_data.get_data_for_key(i) # 从依赖接口的返回结果中匹配需要的依赖字段 request_data[depend_request] = depend_response if header == 'yes': self.oper_header.write_cookie() cookies = self.oper_header.get_cookie() header = {'Cookie': cookies} # json.dump() res = self.run.run_main(url, method, request_data, header) else: res = self.run.run_main(url, method, request_data) # if self.comm.contrast(expected, res): # print("测试通过") # self.data.write_result(i, 'pass') # pass_count.append(i) # else: # print("测试失败") # self.data.write_result(i, res) # fail_count.append(i) # self.send_mail.send_main(pass_count,fail_count) def run_request_data(self, index, value): request_data = self.data.get_request_for_json(index) key = self.data.get_fix_request_key(index) request_data[key] = value return request_data def run_expected_data(self, index, value): key = self.data.get_expected_result(index) expected_data = {} expected_data[key] = value expected_data = key + ":" + expected_data[key] return expected_data
def __init__(self, file_name=None, sheet_id=None): self.oper_ini = OperationIni() self.oper_h = OperationHeader() self.run = RunMethod() self.data = GetData(file_name, sheet_id) self.vdc = OperationVDC() self.accept = None
def go_on_run(self): # 10 0,1,2,3, res = None pass_count = [] fail_count = [] 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) print(i, url) method = self.data.get_request_method(i) request_data = self.data.get_data_for_json(i) expect = self.data.get_expect_data(i) header = self.data.is_header(i) depend_case = self.data.is_depend(i) # res = self.run_method.run_main(method, url, data, header) # if is_run: # method,url,data=None,header=None if depend_case != None: # self.depend_data = DependentData(depend_case) print("depend_case is" + " --- " + depend_case) self.depend_data = DependentData(depend_case) # 获取的依赖响应数据 depend_response_data = self.depend_data.get_data_for_key(i) # 获取依赖的key print(depend_response_data) depend_key = self.data.get_depend_field(i) print(depend_key) request_data[depend_key] = depend_response_data if header == "write": res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_cookie() elif header == 'yes': op_json = OperationJson('../dataconfig/cookie.json') cookie = op_json.get_data('apsid') cookies = {'apsid': cookie} # cookie = op_header.get_data() # cookie = ../dataconfig/cookie.json ../dataconfig/cookie.json res = self.run_method.run_main(method, url, request_data, cookies) else: res = self.run_method.run_main(method, url, request_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') pass_count.append(i) else: # print("测试失败") # self.data.write_result(i, 'fail') self.data.write_result(i, res) fail_count.append(i) self.send_email.send_main(pass_count, fail_count)
def go_on_run(self): """程序执行""" pass_count = [] fail_count = [] res = None # 获取用例数 rows_count = self.data.get_case_lines() # 第一行索引为0 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 = DependentData(depend_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 # 如果header字段值为write则将该接口的返回的token写入到token.json文件,如果为yes则读取token.json文件 if header == "write": res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_token() elif header == 'yes': op_json = OperationJson("../dataconfig/token.json") token = op_json.get_data('data') request_data = dict(request_data, **token) # 把请求数据与登录token合并,并作为请求数据 res = self.run_method.run_main(method, url, request_data) else: res = self.run_method.run_main(method, url, request_data) if expect != None: if self.com_util.is_contain(expect, res): self.data.write_result(i, "Pass") pass_count.append(i) else: self.data.write_result(i, res) fail_count.append(i) else: print("用例ID:case-{i},预期结果不能为空") # 发送邮件 # self.send_email.send_main(pass_count, fail_count) print("通过用例数:{len(pass_count)}") print("失败用例数:{len(fail_count)}")
def go_on_run(self): res = None pass_count = [] fail_count = [] #10 0,1,2,3 rows_count = self.data.get_case_lines() 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_for_mysql(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) #获取的依赖响应数据 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 if header == 'write': res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_cookie() elif header == 'yes': op_json = OperetionJson('../dataconfig/cookie.json') cookie = op_json.get_data('apsid') cookies = {'apsid': cookie} res = self.run_method.run_main(method, url, request_data, cookies) else: res = self.run_method.run_main(method, url, request_data) print(res) print(type(res)) #if self.com_util.is_equal_dict(expect,res) == 0: #if self.com_util.is_contains_dict(expect,res): if self.com_util.is_contain(expect, res): self.data.write_result(i, 'pass') pass_count.append(i) res = pass_count else: #self.data.write_result(i,res) self.data.write_result(i, 'fail') fail_count.append(i) res = fail_count
def go_on_run(self): # 程序执行 pass_count = [] fail_count = [] rows_count = self.data.get_case_lines() 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) data = self.data.get_data_for_json(i) # expect = self.data.get_expect_data(i) # Excel写入预期结果 expect = self.data.get_expect_data_for_mysql(i) header = self.data.is_header(i) depend_case = self.data.is_depend(i) res = self.run_method.run_main(method, url, data, header) values = {"resultCode": res["resultCode"]} # print(res["resultData"]["access_id"]) value = json.dumps(values) # 字典转字符串匹配 if depend_case is not None: self.depend_data = DependentDate(depend_case) # 依赖响应数据 depend_response_data = self.depend_data.get_data_for_key(i) # 获取依赖的key data = json.loads(data) depend_key = self.data.get_depend_field(i) data[depend_key] = depend_response_data if header == 'write': res = self.run_method.run_main(method, url, data) op_header = OperationHeader(res) op_header.write_cookie() elif header == 'yes': op_json = OperationJson('../dataconfig/cooking.json') cookie = op_json.get_data('apsid') cookies = { 'apsid': cookie } value = self.run_method.run_main(method, url, data, cookies) else: value = self.run_method.run_main(method, url, data) if self.commonutil.is_equal_dict(expect, value): self.data.write_result(i, '通过') pass_count.append(i) print('测试通过') else: self.data.write_result(i, '失败') fail_count.append(i) print('测试失败') print(pass_count, fail_count) self.sendmail.send_main(pass_count, fail_count)
def go_on_run(self): res = None pass_count = [] fail_count = [] # 10 0,1,2,3 rows_count = self.data.get_case_lines() 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) # 获取的依赖响应数据 depend_response_data = self.depend_data.get_data_for_key(i) # 获取依赖的key depend_key = self.data.get_depend_field(i) #print(request_data) #request_data[depend_key] = depend_response_data request_data = self.operation.deal_data(request_data, depend_key, depend_response_data) if header == 'write': res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_cookie() elif header == 'yes': op_json = OperetionJson('config/cookie.json') cookie = op_json.get_data('apsid') cookies = { 'apsid': cookie } res = self.run_method.run_main(method, url, request_data, cookies) else: res = self.run_method.run_main(method, url, request_data, {'Content-Type': 'application/x-www-form-urlencoded'}) if self.com_util.is_equal_dict(expect, res): self.data.write_result(i, 'pass') pass_count.append(i) else: self.data.write_result(i, res) fail_count.append(i) self.send_mai.send_email(pass_count, fail_count)
def go_on_run(self): res = None pass_count = [] fail_count = [] #10 0,1,2,3 rows_count = self.data.get_case_lines() 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) case_name = self.data.get_case_name(i) if depend_case != None: self.depend_data = DependdentData(depend_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 if header == 'write': res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_cookie() elif header == 'yes': op_json = OperetionJson('./dataconfig/cookie.json') cookie = op_json.get_data('apsid') # cookies = { # 'apsid':cookie # } res = self.run_method.run_main(method, url, request_data, cookie) self.logger.info("用例名称:" + case_name + "--->返回值:" + res) #print(res) else: res = self.run_method.run_main(method, url, request_data) self.logger.info("用例名称:" + case_name + "--->返回值:" + res) #print(res) if self.com_util.is_contain(expect, res) == True: self.data.write_result(i, 'pass') pass_count.append(i) else: self.data.write_result(i, res) fail_count.append(i) #self.send_mai.send_main(pass_count,fail_count) #不带文件发送 self.send_email.sendemail(pass_count, fail_count) #带文件发送
def go_on_run(self): res = None pass_count = [] fail_count = [] #10 0,1,2,3 rows_count = self.data.get_case_lines() 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_for_mysql(i) expect = self.data.get_expcet_data(i) # sql_data = expect.strip("[]") sql_data1 = eval(expect) # sql_result = eval(sql_data1) header = self.data.is_header(i) depend_case = self.data.is_depend(i) if depend_case != None: self.depend_data = DependdentData(depend_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 # print(request_data[depend_key]) # res = self.run_method.run_main(method,url,request_data) if header == 'write': res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_cookie() elif header == 'yes': res = self.run_method.run_main(method, url, request_data) # print(5,res) else: res = self.run_method.run_main(method, url, request_data) print(4, res) if self.com_util.is_equal_dict(res, sql_data1): self.data.write_result(i, 'pass') pass_count.append(i) else: self.data.write_result(i, str(res)) fail_count.append(i) #将运行结果通过钉钉发送 dd_mes(pass_count, fail_count) exit()
def go_on_run(self): res = None pass_count = [] fail_count = [] #10 0,1,2,3 depend_data = '' rows_count = self.data.get_case_lines() 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_request_data(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) #获取的依赖响应数据 depend_response_data = self.depend_data.get_data_for_key(i) depend_data = depend_response_data #获取依赖的key depend_key = self.data.get_depend_field(i) #数据依赖自动未填写,则不处理;请求自动无数据依赖字段,则不处理 if depend_key != None and request_data.haskey(depend_key): request_data[depend_key] = depend_response_data if header == None: res = self.run_method.run_main(method, url, request_data) elif header == 'write': res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_header() #op_header.write_cookie() else: res = self.run_method.run_main(method, url, request_data, header) #函数判断 if expect.find('${') >= 0: self.data.find_def(expect) elif self.com_util.is_contain(expect, res): self.data.write_result(i, 'pass') pass_count.append(i) else: self.data.write_result(i, res) fail_count.append(i)
def go_on_run(self): res = None pass_count = [] fail_count = [] #10 0,1,2,3 rows_count = self.data.get_case_lines() 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_for_mysql(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) #获取依赖的响应数据 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 if header == 'write': res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_cookie() elif header == 'yes': op_json = OperetionJson('../dataconfig/cookie.json') cookie = op_json.get_data('apsid') cookies = {'apsid': cookie} res = self.run_method.run_main(method, url, request_data, cookies) else: res = self.run_method.run_main(method, url, request_data) # print(res["data"][2]["context"]) print(res) # print(type(res)) #自己写的:这个是判断字段是否在结果中 if expect in res: self.data.write_result(i, "pass") pass_count.append(i) else: self.data.write_result(i, "fail") fail_count.append(i)
def run(self): res = None pass_count = [] fail_count = [] rows_count = self.data.get_case_lines() for i in range(1, rows_count): run = self.data.get_run(i) if run: url = self.data.get_url(i) request_method = self.data.get_request_method(i) request_data = self.data.get_data_for_json(i) expect = self.data.get_expect(i) print(expect) header = self.data.get_header(i) # case_depend = self.data.get_case_depend(i) # if case_depend != None: # self.depend_data = DependentData(case_depend) # depend_response_data = self.depend_data.get_data_for_key(i) # depend_key = self.data.get_depend_response_data(i) # request_data[depend_key] = depend_response_data if header == 'write': res = self.run_method.run_main(request_method, url, request_data) op_header = OperationHeader(res) op_header.write_cookie() elif header == 'yes': op_json = OperationJson('../dataconfig/cookie.json') cookie = op_json.get_data('apsid') cookies = {'apsid': cookie} res = self.run_method.run_main(request_method, url, request_data, cookies) else: res = self.run_method.run_main(request_method, url, request_data) print(res) if self.common_util.is_contain(expect, res) == 1: self.data.write_result(i, 'pass') pass_count.append(i) print(pass_count) else: self.data.write_result(i, res) fail_count.append(i) print(fail_count) self.send_email.send_main(pass_count, fail_count)
def test_main(self, sheet_name, case_name, sheet_id, case_id): self.data = GetData(sheet_id) is_run = self.data.get_is_run(case_id) if is_run: url = self.data.get_request_url(case_id) method = self.data.get_request_method(case_id) #request_data = self.data.get_data_for_json(i) request_data = self.data.get_request_data(case_id) # expect = self.data.get_expcet_data_for_mysql(i) expect = self.data.get_expcet_data(case_id) header = self.data.is_header(case_id) depend_case = self.data.is_depend(case_id) if depend_case != None: self.depend_data = DependdentData(sheet_id, depend_case) # 获取的依赖响应数据 depend_response_data = self.depend_data.get_data_for_key(case_id) # 获取依赖的key depend_key = self.data.get_depend_field(case_id) request_data[depend_key] = depend_response_data if header == 'write': res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_cookie() elif header == 'yes': op_json = OperetionJson('../dataconfig/cookie.json') cookie = op_json.get_data('apsid') cookies = { 'apsid': cookie } res = self.run_method.run_main(method, url, request_data, cookies) else: res = self.run_method.run_main(method, url, request_data) print(res) if self.com_util.is_contain(expect, res): self.data.write_result(case_id,'pass') else: self.data.write_result(case_id, res) #self.assertIn(expect,res,"预期结果:{0} 未在实际返回结果:{1} 中! ".format(expect,res)) #添加最后结果时间戳 self.data.write_result(0, '实际结果 {}'.format(time.strftime("%Y_%m_%d_%H_%M_%S"))) self.assertIn(expect,res)
def go_on_run(self): res = None pass_count = [] fail_count = [] # 10 0,1,2,3 rows_count = self.data.get_case_lines() 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_for_mysql(i) header = self.data.is_header(i) depend_case = self.data.is_depend(i) if depend_case != None: self.depend_data = DependdentData(depend_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 if header == 'write': res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_cookie() elif header == 'yes': op_json = OperetionJson('../dataconfig/cookie.json') cookie = op_json.get_data('apsid') cookies = { 'apsid': cookie } res = self.run_method.run_main(method, url, request_data, cookies) else: res = self.run_method.run_main(method, url, request_data) if self.com_util.is_equal_dict(expect, res) == 0: self.data.write_result(i, 'pass') pass_count.append(i) else: self.data.write_result(i, res) fail_count.append(i) self.send_mai.send_main(pass_count, fail_count)
def go_on_run(self): res = None pass_count = [] fail_count = [] rowcount = self.data.get_case_rowCount() for i in range(1, rowcount): is_run = self.data.get_is_run(i) if is_run: modname = self.data.get_module(i) url = self.data.get_url(i) method = self.data.get_request_method(i) data = self.data.get_data_for_json(i) is_cookie = self.data.get_cookies(i) header = self.data.is_header(i) hoperesult = self.data.get_hopereval(i) is_depend = self.data.is_depend(i) # 是否需要依赖 if is_depend != None: self.depend_data = DependData(is_depend) # 获取依赖的响应数据 depend_response_data = self.depend_data.get_data_for_key(i) # 获取依赖的key depend_key = self.data.get_depend_field(i) data[depend_key] = depend_response_data # 是否保存cookies if is_cookie == 'write': res = self.run_method.run_main(method, url, data) op_headers = OperationHeader(res) if modname == "登录": op_headers.write_login_cookie() else: op_headers.write_cookie() elif is_cookie == 'yes': op_json = OperationJson('../case/cookie.json') header = {"Cookie": op_json.data} res = self.run_method.run_main(method, url, data, header) else: res = self.run_method.run_main(method, url, data) res = res.content.decode('utf-8') # print(res) if self.commonUtil.is_contain(hoperesult, res): self.data.write_realresult(i, res) self.data.write_result(i, "测试成功") pass_count.append(i) else: self.data.write_realresult(i, res) self.data.write_result(i, "测试失败") fail_count.append(i) res = json.loads(res) if (res['statusCode'] != "200"): # print(res['errorMsg']) pass
class ApiBase(object): def __init__(self, file_name=None, sheet_id=None): self.oper_ini = OperationIni() self.oper_h = OperationHeader() self.run = RunMethod() self.data = GetData(file_name, sheet_id) self.vdc = OperationVDC() self.accept = None def get_host(self): ip = self.oper_ini.get_value('host', 'url') return ip def get_flag(self, rowx): return self.data.get_flag(rowx) def get_path_xls(self, rowx): return self.data.get_url(rowx) def get_url_xls(self, rowx, vapp_id=None): ip = self.get_host() path_xls = self.get_path_xls(rowx) vdc_id = self.get_vdc_id() flag = self.get_flag(rowx) url = None # 根据flag判断是否需要修改url信息:0为不需要,1是需要替换vdc_id ,2是需要替换vdc_id,vapp_id if flag == 0: url = ip + path_xls elif flag == 1: path = path_xls % vdc_id url = ip + path elif flag == 2: path = path_xls % (vdc_id, vapp_id) url = ip + path return url def get_vdc_id(self): return self.vdc.get_vdc_id() def get_headers(self, accept=None, content_type=None): headers = self.oper_h.get_headers(accept, content_type) return headers def get_request_data(self, rowx): return self.data.get_request_for_json(rowx) def request_get(self, path, headers): url = self.get_url_xls(path) res = self.run.run_main(url, 'GET', headers=headers) print(res)
class OperationVDC(object): def __init__(self): self.oper_ini = OperationIni() self.oper_h = OperationHeader() self.run = RunMethod() def request_virtualdatacenters(self): url = self.oper_ini.get_value('host', 'url') + '/api/cloud/virtualdatacenters' accept = 'application/vnd.esage.virtualdatacenters+json;version=3.0' headers = self.oper_h.get_headers(accept=accept) res = self.run.run_main(url, 'GET', headers=headers) id = res['collection'][0]['id'] return id def write_vdc_id(self): vdc_id = self.request_virtualdatacenters() self.oper_ini.write_value('global', 'virtualdatacenters', str(vdc_id)) def get_vdc_id(self): vdc_id = self.oper_ini.get_value('global', 'virtualdatacenters') return vdc_id
def go_on_run(self): global expect expect = True res = None pass_count = [] fail_count = [] # 10 0,1,2,3 rows_count = self.data.get_case_lines() 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_for_mysql(i) expect = self.data.get_expcet_data(i) header = self.data.is_header(i) depend_case = self.data.is_depend(i) if depend_case is not None: self.depend_data = DependdentData(depend_case) # 获取的依赖响应数据 # for depend_response_data_list in depend_response_data = self.depend_data.get_data_for_key(i) if len(depend_response_data) == 1: request_data_result = "".join(depend_response_data) else: request_data_result = ",".join( [str(i) for i in depend_response_data]) # 获取依赖的key depend_key = self.data.get_depend_field(i) depend_key_list = depend_key.split(",") print(type(depend_key_list)) for (key_list, key) in zip(request_data_result.split(","), depend_key_list): print(key_list, key) if method == "Post" or method == "Get" or method == "Put_Add" or method == "Put": # print(key) request_data[key] = key_list else: request_data = depend_response_data break contentType = "application/json" if header == 'write': header = {'Content-Type': contentType} res = self.run_method.run_main(method, url, data=request_data, header=header) op_header = OperationHeader(res) op_header.write_token() elif header == 'yes': op_json = OperetionJson('../dataconfig/token.json') token = op_json.get_data('token') header = { 'Content-Type': contentType, 'Authorization': token } # print("request_data=======%s" % request_data) res = self.run_method.run_main(method, url, request_data, header=header) elif header == 'write_user': header = { 'Content-Type': contentType, } res = self.run_method.run_main(method, url, request_data, header=header) op_header = OperationHeader(res) op_header.write_user_token() elif header == 'weChat': op_json = OperetionJson('../dataconfig/weChatToken') token = op_json.get_data('token') header = { 'Content-Type': contentType, 'Authorization': token } res = self.run_method.run_main(method, url, request_data, header=header) else: res = self.run_method.run_main(method, url, request_data) if self.com_util.is_contain(expect, res) == 0: self.data.write_result(i, 'pass') pass_count.append(i) else: self.data.write_result(i, res) fail_count.append(i)
def setUpClass(cls): oper_header = OperationHeader() oper_header.is_cookie()
def go_on_run(self): res = None # 定义三个列表,存储成功,失败,未执行的用例(case_id) pass_count = [] fail_count = [] no_run_count = [] # 每次执行用例之前将log日志文件清空数据 log_file = "D:\GZRJ\Python_XM\\api_automation_test\log\log.txt" with open(log_file,'w') as f: f.seek(0,0) # 加上f.seek(0),把文件定位到position 0;没有这句的话,文件是定位到数据最后,truncate也是从最后这里删除 f.truncate() # 先获取excel表格的行数 row_count = self.data.get_case_lines() print("start_用例开始执行,请稍候.....") for i in range(1, row_count): # 每循环一次,拿一次接口的所有数据 try: # 先拿到is_run判断是否执行,如果是yes(会返回Ture),就去拿其它数据 is_run = self.data.get_is_run(i) if is_run == True: method = self.data.get_request_method(i) url = self.data.get_request_url(i) data = self.data.get_data_for_json(i) header_key = self.data.is_header(i) # 获取excel文件中header关键字 header = self.data.get_header_value(i) # 获取json文件中header_key对应的头文件数据 expect = self.data.get_expcet_data(i) # 这里拿到的预期结果是字符串 # expect = self.data.get_expcet_data_for_mysql(i) # 断言方法如果是is_equal_dict就要取数据库这个值 depend_case = self.data.is_depend(i) # 获取依赖case_id # 判断是否有依赖id,然后把返回数据替换到请求数据 if depend_case != None: self.depend_data = DependdetData(depend_case) # 1.获取依赖的响应字段数据 depend_response_data = self.depend_data.get_data_for_key(i) # 2.获取需要替换的字段 depend_key = self.data.get_depend_field(i) # 3.把1步拿到的values去替换第2步拿到的字段 data[depend_key] = depend_response_data # 判断是否要写入,获取cookie if header_key == "write_Cookies": res = self.run_method.run_main(method, url, data, header, params=data) op_header = OperationHeader(res) op_header.write_cookie() res = res.json() # 先获取cookies再转成json字典类型,不然后面无法和预期结果对比 elif header_key == "get_Cookies": op_json = OperationJson("D:\GZRJ\Python_XM\\api_automation_test\dataconfig\cookie.json") cookie = op_json.get_data("PHPSESSID") cookies = {"PHPSESSID": cookie} #print(cookies) res = self.run_method.run_main(method, url, data, header, params=data, cookies=cookies).json() else: res = self.run_method.run_main(method, url, data, header, params=data).json() #print(res) #print(res["data"]["accessToken"]) # 判断预期结果是否在返回结果里面,需要把返回结果res转成json字符串中文和预期结果比较 if self.com_util.is_contain(expect, json.dumps(res, ensure_ascii=False)): self.data.write_result(i, "pass") pass_count.append(i) else: self.data.write_result(i, json.dumps(res, ensure_ascii=False)) with open(log_file, 'a', encoding='utf-8') as f: f.write("\n第%s条用例实际结果与预期结果不一致:\n" % i) f.write("Expected:%s\n Actual:%s\n" % (expect, res)) fail_count.append(i) # 如果is_run是no,会返回False,然后把当前的case_id(行号)写入no_run_count else: no_run_count.append(i) except Exception as e: # 将异常写入excel的测试结果中 self.data.write_result(i, str(e)) # 将报错写入指定路径的日志文件里 with open(log_file, 'a', encoding='utf-8') as f: f.write("\n第%s条用例报错:\n" % i) initLogging(log_file, e) fail_count.append(i) """ if self.com_util.is_equal_dict(expect, res): self.data.write_result(i, "PASS") pass_count.append(i) else: self.data.write_result(i, res) fail_count.append(i) """ #我这里打印了一下用例的执行情况,嘿嘿 print("end_用例执行完毕!") print("成功的用例:%s" % pass_count) print("失败的用例:%s" % fail_count)
def __init__(self, file_name=None, sheet_id=0): self.data = GetData(file_name, sheet_id) self.oper_header = OperationHeader() self.run = RunMethod() self.comm = Common() self.send_mail = SendEmail()
def go_on_run(self): """程序执行""" pass_count = [] fail_count = [] request_data_file = '' res = None # 获取用例数 rows_count = self.data.get_case_lines() # 第一行索引为0 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 = json.load(self.data.get_request_data(i)) expect = self.data.get_expcet_data(i) token = self.data.is_token(i) depend_case = self.data.is_depend(i) headers = self.data.get_headers(i) r = self.data.get_request_data(i) # 上传文件处理 if r != None: if r.endswith('jpg') | r.endswith('png') | r.endswith( 'docx') | r.endswith('doc') | r.endswith( 'ppt') | r.endswith('pptx'): #其他文件暂不考虑 file_payload = { 'file': (r, open('../' + r, 'rb')) } #, "image/jpg" m = MultipartEncoder(file_payload) headers['Content-Type'] = m.content_type request_data = m #处理依赖 elif depend_case != None: self.depend_data = DependentData(depend_case) # 获取依赖key和value [id1:3] depend_response_data = self.depend_data.get_data_for_key( i) # {caseid:{id1:1,id2:2}} for caseid in depend_response_data: for k in depend_response_data[caseid]: y = '${' + caseid + ',' + k + '}' if r.find(y): t = r.replace( y, str(depend_response_data[caseid][k])) r = t request_data = json.loads(r, encoding='utf-8') else: # 没有依赖直接转换输出 request_data = json.loads(r, encoding='utf-8') else: request_data = {} # 如果token字段值为write则将该接口的返回的token写入到token.json文件,如果为yes则读取token.json文件 if token == "write": res = self.run_method.run_main(method, url, request_data, headers) op_header = OperationHeader(res) op_header.write_token() elif token == 'yes': op_json = OperationJson("../dataconfig/token.json") token = op_json.get_data('data') headers.update(token) #request_data = dict(request_data, **token) # 把请求数据与登录token合并,并作为请求数据 res = self.run_method.run_main(method, url, request_data, headers) else: res = self.run_method.run_main(method, url, request_data, headers) #将响应数据写入excel log().info("响应结果\n%s", res) self.data.write_respond_data(i, res) if expect != None: if self.com_util.is_contain(expect, res): self.data.write_result(i, "Pass") pass_count.append(i) else: self.data.write_result(i, "failed") fail_count.append(i) else: print(f"用例ID:case-{i},预期结果不能为空") # 发送邮件 # self.send_email.send_main(pass_count, fail_count) print(f"通过用例数:{len(pass_count)}") print(f"失败用例数:{len(fail_count)}")
def go_on_run(self): count_all_case = 0 count_all_case_detail = [] count_yes_case = 0 count_yes_case_detail = [] count_no_case = 0 count_no_case_detail = [] pass_count = [] fail_count = [] #通过data_json_of_excel来获取json文件里面保存的excel文件名,然后根据字典里面的value值是否为yes来确定执行相应的excel get_json_of_excel = self.data_json_of_excel.get_json_of_excel() #这里的for循环是为了控制遍历执行json文件中的所有需执行的excel文件 for jsonOfExcelDict in get_json_of_excel: if get_json_of_excel[jsonOfExcelDict].lower() == 'yes': excel_path = '../dataconfig/' + jsonOfExcelDict + '.xls' self.data = GetData(excel_path, 0) self.run_method.setExcelPathName(excel_path) else: continue res = None #10 0,1,2,3 rows_count = self.data.get_case_lines() #置空上次接口测试的结果 # for i in range(1, rows_count): # self.data.write_void_to_output_cell(i, " ") # print(self.data.get_case_name(i)) #开始接口测试 for i in range(1, rows_count): count_all_case += 1 is_run = self.data.get_is_run(i) if is_run: count_yes_case += 1 case_id = self.data.get_case_id(i) case_name = self.data.get_case_name(i) # 显示当前正在执行的case的id和name,用来在报错的时候定位是那一条case出问题了 print("ID:\n{}\n接口名称:{}\n调用表路径:{}".format( case_id, case_name, excel_path)) url = self.data.get_request_url(i) method = self.data.get_request_method(i) request_data = self.data.get_data_for_json(i) #这是mss老师当时把预期结果里面的sql语111句查询出来,然后返回到expect里面的写法 # expect = self.data.get_one_sql_data(i, self.data.get_expcet_data) expect = self.data.get_expcet_data(i) # RunTestPrint # print(expect) # print(type(expect)) header = self.data.is_header(i) depend_case = self.data.is_depend(i) depend_case_sql = self.data.is_depend_sql(i) if depend_case != None: self.depend_data = DependdentData( depend_case, excel_path, 0) #获取的依赖响应数据 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 if depend_case_sql != None: #获取的sql查询依赖数据 depend_sql_results = self.data.get_one_sql_data( i, self.data.get_sql_rely_result_data) #如果depend_sql_results的值为null说明这段sql语句没有查询出结果,比如我在做某个用户新增之前我需要先删除, #但是如果该用户根本都没有的话,那么就删除不了,这时候就需要这里的if来判断 if depend_sql_results != "null": #把unicode格式的字典改为json格式 depend_sql_results = json.loads(depend_sql_results) #通过循环把字典数据里面的value取出来 for key in depend_sql_results: depend_sql_result = depend_sql_results[key] #获取依赖的key depend_key = self.data.get_depend_field(i) request_data[depend_key] = depend_sql_result if header == 'write': res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_header() elif header == 'yes': op_json = OperetionJson('../dataconfig/cookie.json') cookie = op_json.get_data('token') cookies = {"Authorization": cookie} try: res = self.run_method.run_main( method, url, request_data, cookies) except json.decoder.JSONDecodeError: # 如果出现json.decoder.JSONDecodeError的报错的话,就走到这里,然后把响应的模块清空 # 这样子的话我就知道哪些模块出错了,或者说知道哪些模块没有执行 self.data.write_void_to_output_cell(i, " ") print(self.data.get_case_name(i)) continue else: try: res = self.run_method.run_main( method, url, request_data) except json.decoder.JSONDecodeError: # 如果出现json.decoder.JSONDecodeError的报错的话,就走到这里,然后把响应的模块清空 # 这样子的话我就知道哪些模块出错了,或者说知道哪些模块没有执行 self.data.write_void_to_output_cell(i, " ") print(self.data.get_case_name(i)) continue #写响应结果返回值到返回状态列 self.data.write_code_responde_result( i, json.loads(res)['code']) #写请求数据内容到请求数据内容列 self.data.write_request_data_result( i, json.dumps(request_data, ensure_ascii=False)) #判断预期结果和实际结果是否一致,并将结果填入excel if self.com_util.is_equal_dict2(expect, res) == True: self.data.write_result_status(i, 'pass') self.data.write_result(i, res) pass_count.append(i) else: self.data.write_result_status(i, 'fail') self.data.write_result(i, res) fail_count.append(i) sql_result = self.data.get_all_sql_data( i, self.data.get_sql_result_data) self.data.write_sql_result(i, sql_result) else: count_no_case_detail.append(self.data.get_case_name(i)) self.send_mai.send_main(pass_count, fail_count, "OA项目所有空参数测试点") print('所有case条数为:{}条;\n运行了的case条数为{}条;\n没有运行的case条数为{}条.'.format( count_all_case, count_yes_case, count_all_case - count_yes_case)) print('(因为登录模块多执行了9次,所以所有case真是条数条数应该为{}-9={})'.format( count_all_case, count_all_case - 9)) print('没有运行的case是:') for case_detail in count_no_case_detail: print(case_detail)
def go_on_run(self): res = None pass_count = [] fail_count = [] #10 0,1,2,3 rows_count = self.data.get_case_lines() 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_for_mysql(i) expect = self.data.get_expcet_data(i) # print("except data is %s" %expect) header = self.data.is_header(i) depend_case = self.data.is_depend(i) response_value = self.data.get_response_result_field(i) is_post_previous_case = self.data.get_post_result(i) depend_excel_file = self.data.is_excel(i) if depend_case == None: if header == 'write': res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_cookie() elif header == 'yes': op_json = OperetionJson('../dataconfig/cookie.json') cookie = op_json.get_data('apsid') cookies = {'apsid': cookie} headers1 = op_json.get_data('Authorization') headers = {'Authorization': headers1} if depend_excel_file == 'Y': files = { 'file': ('new1.xlsx', open("C:\\Users\\Aaron\\Desktop\\new1.xlsx", 'rb'), 'application/vnd.ms-excel') } res = self.run_method.run_main( method, url, request_data, headers, files, cookies) else: convert_data = json.dumps(request_data) res = self.run_method.run_main(method, url=url, data=convert_data, headers=headers, cookies=cookies) else: res = self.run_method.run_main(method, url, request_data) # res = self.run_method.run_main(method, url, request_data) elif depend_case == "write": if header == 'write': res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_cookie() elif header == 'yes': op_json = OperetionJson('../dataconfig/cookie.json') cookie = op_json.get_data('apsid') cookies = {'apsid': cookie} headers1 = op_json.get_data('Authorization') headers = {'Authorization': headers1} if depend_excel_file == 'Y': files = { 'file': ('new1.xlsx', open("C:\\Users\\Aaron\\Desktop\\new1.xlsx", 'rb'), 'application/vnd.ms-excel') } res = self.run_method.run_main( method, url, request_data, headers, files, cookies) else: res = self.run_method.run_main( method, url, request_data, headers, cookies) else: res = self.run_method.run_main(method, url, request_data) # res = self.run_method.run_main(method, url, request_data) self.data.write_response_to_cell( i, res.content.decode("utf-8")) else: self.depend_data = DependdentData(depend_case) # 获取依赖的key depend_key = self.data.get_depend_field(i) if is_post_previous_case == 'N': depend_response_data = self.depend_data.get_data_for_excel( i) else: #获取的依赖响应数据 depend_response_data = self.depend_data.get_data_for_key( i) request_data[depend_key] = depend_response_data if header == 'write': res = self.run_method.run_main(method, url, request_data) op_header = OperationHeader(res) op_header.write_cookie() elif header == 'yes': op_json = OperetionJson('../dataconfig/cookie.json') cookie = op_json.get_data('apsid') cookies = {'apsid': cookie} headers1 = op_json.get_data('Authorization') headers = {'Authorization': headers1} files = { 'file': ('new1.xlsx', open("C:\\Users\\Aaron\\Desktop\\new1.xlsx", 'rb'), 'application/vnd.ms-excel') } request_data = json.dumps(request_data) print(type(request_data)) res = self.run_method.run_main(method, url=url, data=request_data, headers=headers, cookies=cookies) else: res = self.run_method.run_main(method, url, request_data) # if depend_case == "write": # self.data.write_response_to_cell(i,res.decode("utf-8")) # if self.com_util.is_equal_dict(expect,json.dumps(res)) == 0: if self.com_util.is_contain( expect, res.content.decode("utf-8")) == True: self.data.write_result(i, 'pass') pass_count.append(i) else: # self.data.write_result(i,json.dumps(res,ensure_ascii=False)) self.data.write_result(i, res.content.decode("utf-8")) fail_count.append(i) self.send_mai.send_main(pass_count, fail_count)
def go_on_run(self): """程序执行""" pass_count = [] fail_count = [] no_run_count = [] res = None # 获取用例数 rows_count = self.data.get_case_lines() print(rows_count) # 第一行索引为0 for i in range(1, rows_count): print("i:", i) is_run = self.data.get_is_run(i) print(is_run) if is_run: url = self.data.get_request_url(i) print("url:", url) method = self.data.get_request_method(i) print("method:", method) request_data = self.data.get_data_for_json(i) print("request_data:", request_data) print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", type(request_data)) expect = self.data.get_expect_data(i) print("expect:", expect) header = self.data.is_header(i) print("header:", header) token = self.data.is_token(i) print("token:", token) depend_case = self.data.is_depend(i) print("depend_case:", depend_case) if depend_case != None: self.depend_data = DependentData(depend_case) # 获取依赖的响应数据 print("#### i", i) depend_response_data = self.depend_data.get_data_for_key(i) print("##################") print("1depend_response_data:", depend_response_data) # 获取依赖的key depend_key = self.data.get_depend_field(i) print("depend_key:", depend_key) # 更新请求字段 request_data.get( 'params')[depend_key] = depend_response_data #request_data = json.dumps(request_data, ensure_ascii=False) print("request_data:", request_data) print(type(request_data)) # 如果header字段值为write则将该接口的返回的token写入到token.json文件,如果为yes则读取token.json文件 if header == "write_token": print("********************") res = self.run_method.run_main(method, url, request_data).json() print("res:", res) print("res type", type(res)) #print("res.json():", res.json()) op_header = OperationHeader(res) op_header.write_token() elif header == 'write_cookie' and token == 'yes': print("case22222222222222") op_json = OperationJson("../dataconfig/token.json") token = op_json.get_data('data') request_data = dict(request_data, **token) # 把请求数据与登录token合并,并作为请求数据 print("login_data", request_data) res = self.run_method.run_main(method, url, request_data) print("res_login:"******"@@@@@@@@@@@@@@@@@@@@@@@@@@") op_json1 = OperationJson('../dataconfig/cookie.json') op_json2 = OperationJson('../dataconfig/token.json') cookies = op_json1.get_data('cookie') token = op_json2.get_data('data') request_data = dict(request_data, **token) print("post_data:", request_data) print("cookie:", cookies) new_value = json.dumps(request_data.get("params")) request_data["params"] = new_value res = self.run_method.run_main(method, url, request_data, cookies=cookies) print("res:", type(res)) else: res = self.run_method.run_main(method, url, request_data) if expect != None: if self.com_util.is_contain(expect, res): self.data.write_result(i, "Pass") pass_count.append(i) if type(res) is dict: self.data.write_real_res(i, json.dumps(res)) else: self.data.write_real_res(i, json.dumps(res.json())) else: self.data.write_result(i, "Fail") fail_count.append(i) if type(res) is dict: self.data.write_real_res(i, json.dumps(res)) else: self.data.write_real_res(i, json.dumps(res.json())) else: print(f"用例ID:case-{i},预期结果不能为空") else: self.data.write_result(i, "Not run") no_run_count.append(i) # 发送邮件 print(f"通过用例数:{len(pass_count)}") print(f"失败用例数:{len(fail_count)}") print(f"未执行败用例数:{len(no_run_count)}") self.send_email.send_main(pass_count, fail_count, no_run_count)
def go_on_run(self): res = None pass_count = [] fail_count = [] no_run_count = [] rows_count = self.data.get_case_lines() #每次执行时清楚log文件 log_file = '../log/log.txt' with open(log_file, 'w') as f: f.seek(0, 0) #定位光标位置 f.truncate() #删除文件 for i in range(1, rows_count): try: 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) #获取请求参数 data = self.data.get_data_value(i) #获取excel文件的关键字 header_key = self.data.get_request_header(i) #获取json文件中header对应的文件数据 header = self.data.get_header_value(i) expect = self.data.get_expect_data(i) depend_case = self.data.is_depend(i) if depend_case != None: self.depend_data = DependentData(depend_case) #获取依赖字段的响应数据 depend_response_data = self.depend_data.get_value_for_key( i) #获取请求依赖的key depend_key = self.data.get_depend_field(i) #将依赖case的响应返回某个字段value赋值给接口请求 data[depend_key] = depend_response_data if header_key == 'write_Cookies': res = self.run_method.run_main(method, url, data, header, params=data) op_header = OperationHeader(res) op_header.write_cookie() elif header_key == 'get_Cookies': op_json = OperationJson('../dataconfig/cookie.json') cookie = op_json.get_data('apsid') cookies = {'apsid': cookie} res = self.run_method.run_main(method, url, data, header=cookies, params=data) else: res = self.run_method.run_main(method, url, data, header, params=data) #get请求参数是params:request.get(url='',params={}),post请求数据是data:request.post(url='',data={}) #excel文件中没有区分直接用请求数据表示,则data = self.data.get_data_value(i)拿到的数据,post请求就是data=data,get请就是params=data #excel拿到的expect数据时str类型,返回的是res是dic类型 if self.com_util.is_contain(expect, json.dumps(res)): self.data.write_result(i, 'pass') pass_count.append(i) else: #返回res是dic类型,res数据写入excel中,需要dict转换为str self.data.write_result(i, json.dumps(res)) with open(log_file, 'a', encoding='utf-8') as f: f.write('\n第%s条用例不相同\n' % i) f.write('expect:%s\n Actual:%s\n' % (expect, res)) fail_count.append(i) else: no_run_count.append(i) except Exception as e: #异常写入测试结果 self.data.write_result(i, str(e)) #报错写入指定路径文件 with open(log_file, 'a', encoding='utf-8') as f: f.write('\n第%s条测试用例' % i) initlogging(log_file, e) fail_count.append(i) self.send_email.send_main(pass_count, fail_count, no_run_count)
def go_on_run(self): res = None pass_count = [] fail_count = [] no_run_count = [] rows_count = self.data.get_case_lines() # 每次执行用例之前将log日志文件清空数据 log_file = '../log/log.txt' with open(log_file, 'w') as f: f.seek(0, 0) # 加上f.seek(0),把文件定位到position 0;没有这句的话,文件是定位到数据最后,truncate也是从最后这里删除 f.truncate() # 清空数据 # 循环执行每行用例 for i in range(1, rows_count): try: 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) # 获取请求参数 data = self.data.get_data_value(i) # 获取excel文件中header关键字 header_key = self.data.get_request_header(i) # 获取json文件中header_key对应的头文件数据 header = self.data.get_header_value(i) expect = self.data.get_expect_data(i) depend_case = self.data.is_depend(i) if depend_case != None: self.depend_data = DependentData(depend_case) # 获取依赖字段的响应数据 depend_response_data = self.depend_data.get_value_for_key(i) # 获取请求依赖的key depend_key = self.data.get_depend_field(i) # 将依赖case的响应返回中某个字段的value赋值给该接口请求中某个参数 data[depend_key] = depend_response_data # cookie相关的没有跑通,代码逻辑是正常的,但是模拟登陆返回一直是非法请求 if header_key == 'write_Cookies': res = self.run_method.run_main(method, url, data, header, params=data) op_header = OperationHeader(res) op_header.write_cookie() elif header_key == 'get_Cookies': op_json = OperationJson('../dataconfig/cookie.json') cookie = op_json.get_data('apsid') cookies = {'apsid': cookie} res = self.run_method.run_main(method, url, data, header=cookies, params=data) else: res = self.run_method.run_main(method, url, data, header, params=data) ''' get请求参数是params:request.get(url='',params={}),post请求数据是data:request.post(url='',data={}) excel文件中没有区分直接用请求数据表示,则data = self.data.get_data_value(i)拿到的数据,post请求就是data=data,get请就是params=data ''' # excel中拿到的expect数据是str类型,但是返回的res是dict类型,两者数据比较必须都是字符类型 if self.com_util.is_contain(expect, json.dumps(res)): self.data.write_result(i, 'pass') pass_count.append(i) else: # 返回的res是dict类型,要将res数据写入excel中,需将dict类型转换成str类型 self.data.write_result(i, json.dumps(res)) with open(log_file, 'a', encoding='utf-8') as f: f.write("\n第%s条用例实际结果与预期结果不一致:\n" % i) f.write("Expected:%s\n Actual:%s\n" % (expect, res)) fail_count.append(i) else: no_run_count.append(i) except Exception as e: # 将异常写入excel的测试结果中 self.data.write_result(i, str(e)) # 将报错写入指定路径的日志文件里 with open(log_file, 'a', encoding='utf-8') as f: f.write("\n第%s条用例报错:\n" % i) initLogging(log_file, e) fail_count.append(i) self.send_mail.send_main(pass_count, fail_count, no_run_count)
def __init__(self): self.oper_ini = OperationIni() self.oper_h = OperationHeader() self.run = RunMethod()
def __init__(self, file_name, sheet_id): self.run = RunMethod() self.oper_header = OperationHeader() self.vapps = virtualAppliances(file_name, sheet_id)
def __init__(self): self.oper_ini = OperationIni() self.oper_h = OperationHeader() self.run_method = RunMethod() self.oper_json = OperationJson()