def run_pre(self, pre_case): """初始化数据""" url = ConfigYaml().get_conf_url() + pre_case[data_key.url] method = pre_case[data_key.method] params_type = pre_case[data_key.params_type] params = pre_case[data_key.params] expect_result = pre_case[data_key.expect_result] headers = pre_case[data_key.headers] cooikes = pre_case[data_key.cookies] # 判断header是否存在,json转义 # if headers: # header = json.loads(headers) # else: # header = headers header = Base.json_parse(headers) # 判断cooikes是否存在,json转义 # if cooikes: # cooike = json.loads(cooikes) # else: # cooike = cooikes cooikes = Base.json_parse(cooikes) res = self.run_api(url, method, params, headers) print("前置用例执行:%s" % res) return res
def run_pre(self, pre_case): # excel中取得用例数据 host = pre_case[data_key.host] method = pre_case[data_key.method] params = pre_case[data_key.params] body = pre_case[data_key.body] headers = pre_case[data_key.headers] cookies = pre_case[data_key.cookies] # 是预设常用的参数时替换下 if headers in global_params.keys(): headers = global_params[headers] if host in global_params.keys(): host = global_params[host] url = host + pre_case[data_key.url] # 数据类型转换为dict headers = Base.json_parse(headers) cookies = Base.json_parse(cookies) params = Base.json_parse(params) body = Base.json_parse(body) # 前置用例请求结果返回 res = Base.run_api(url, method, params, body, headers, cookies) print("前置用例执行结果:%s" % res) return res
def test_run(self, case): # 3、重构函数内容 # data_key = ExcelConfig.DataConfig # run_list第1个用例,用例,key获取values url = ConfigYaml().get_conf_url() + case[data_key.url] print(url) case_id = case[data_key.case_id] case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cookies = case[data_key.cookies] code = case[data_key.code] db_verify = case[data_key.db_verify] # 1、验证前置条件 if pre_exec: pass # 2、找到执行用例 # 前置测试用例 pre_case = data_init.get_case_pre(pre_exec) print("前置条件信息为:%s" % pre_case) pre_res = self.run_pre(pre_case) print(pre_res) headers, cookies = self.get_correlation(headers, cookies, pre_res) header = Base.json_parse(headers) cookie = Base.json_parse(cookies) res = self.run_api(url, method, params, header, cookie) print("测试用例执行:%s" % res) # allure # sheet名称 feature 一级标签 allure.dynamic.feature(sheet_name) # 模块 story 二级标签 allure.dynamic.story(case_model) # 用例ID+接口名称 title allure.dynamic.title(case_id + case_name) # 请求URL 请求类型 期望结果 实际结果描述 desc = "<font color='red'>请求URL: </font> {}<Br/>" \ "<font color='red'>请求类型: </font>{}<Br/>" \ "<font color='red'>期望结果: </font>{}<Br/>" \ "<font color='red'>实际结果: </font>{}".format(url, method, expect_result, res) allure.dynamic.description(desc) # 断言验证 # 状态码,返回结果内容,数据库相关的结果的验证 # 状态码 assert_util = AssertUtil() assert_util.assert_code(int(res["code"]), int(code)) # 返回结果内容 assert_util.assert_in_body(str(res["body"]), str(expect_result)) # 数据库结果断言 Base.assert_db("db_1", res["body"], db_verify)
def test_run(self, case): # data_key = ExcelConfig.DataConfig url = case[data_key.url] case_id = case[data_key.case_id] case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] actual_result = case[data_key.actual_result] beizhu = case[data_key.beizhu] headers = case[data_key.headers] cookies = case[data_key.cookies] code = case[data_key.code] db_verify = case[data_key.db_verify] if pre_exec: # 前置用例 pre_case = data_init.get_case_pre(pre_exec) # print(f"前置条件信息为:{pre_case}") pre_res = self.run_pre(pre_case) headers, cookies = self.get_correlation(headers, cookies, pre_res) # 增加headers header = Base.json_parse(headers) # 增加cookies cookie = Base.json_parse(cookies) request = Request(ConfigYaml().get_config_url()) # params转义json # 验证params有没有内容 res = self.run_api(url, params, method, header, cookie) print(f"执行测试用例:{res}") # allure allure.dynamic.feature(sheet_name) allure.dynamic.story(case_model) allure.dynamic.title(case_id + case_name) desc = f"url:{url}<br> 请求方法:{method}<br> 期望结果:{expect_result}<br> 实际结果:{res}" allure.dynamic.description(desc) # 断言验证 assert_util = AssertUtil() assert_util.assert_code(int(res['code']), code) assert_util.assert_in_body(str(res['body']), str(expect_result)) # 数据库验证 # sql = Base.init_db('db_1') # db_res = sql.fetch_one(db_verify) # log.debug(f"数据库查询结果:{str(db_res)}") # for db_k,db_v in db_res.items(): # res_line = res['body'][db_k] # assert_util.assert_body(res_line, db_v) Base.assert_db(db_name='db_1', res_body=res['body'], db_verify=db_verify)
def test_run(self, case): # 初始化信息,url.data 等数据 url = ConfigYml().get_conf_url() + case[data_key.url] print(url) case_id = case[data_key.case_id] case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] reference = case[data_key.reference] headers = case[data_key.headers] cookies = case[data_key.cookies] code = case[data_key.code] db_verity = case[data_key.db_verity] # 验证前置条件 if pre_exec: pass # 找到执行用例,前置测试用例 pre_case = data_init.get_case_pre(pre_exec) # print("前置条件信息为:%s" % pre_case) pre_res = self.run_pre(pre_case) headers, cookies = self.get_correlation(headers, cookies, pre_res) # 判断headers和cookie是否存在,如果存在json转义,如果不存在无需转义 header = Base.json_parse(headers) cookie = Base.json_parse(cookies) url_params = Base.joint_url(case[data_key.params], case[data_key.url]) res = self.run_api(url, method, url_params, reference, params, header, cookie) print("测试用例执行:%s" % res) # allure allure.dynamic.feature(sheet_name) # sheet名称 feature 一级标签 allure.dynamic.story(case_model) # 模块 story 二级标签 allure.dynamic.title(case_id + case_name) # 用例ID+接口名称 title # 请求URL 请求类型 期望结果 实际结果描述 desc = "<font color='red'>请求URL: </font> {}<Br/>" \ "<font color='red'>请求类型: </font>{}<Br/>" \ "<font color='red'>期望结果: </font>{}<Br/>" \ "<font color='red'>实际结果: </font>{}".format(url, method, expect_result, res) allure.dynamic.description(desc) # 断言验证 assert_util = AssertUtil() assert_util.assert_code(int(res["code"]), int(code)) # 验证状态码 assert_util.assert_in_body(str(res["body"]), str(expect_result)) # 验证返回结果内容 if len(db_verity) > 0: # 数据库结果断言 Base.assert_db("db_1", res["body"], db_verity)
def run_pre(self, pre_case): url = pre_case[data_key.url] method = pre_case[data_key.method] params = pre_case[data_key.params] headers = pre_case[data_key.headers] cookies = pre_case[data_key.cookies] # 增加headers header = Base.json_parse(headers) # 增加cookies cookie = Base.json_parse(cookies) res = self.run_api(url, params, method, header, cookie) print(f"执行前置用例: {res}") return res
def run_pre(self, pre_case): # 初始化数据 pass url = ConfigYml().get_conf_url() + pre_case[data_key.url] method = pre_case[data_key.method] params = pre_case[data_key.params] reference = pre_case[data_key.reference] headers = pre_case[data_key.headers] cookies = pre_case[data_key.cookies] # 判断headers和cookie是否存在,如果存在json转义,如果不存在无需转义 header = Base.json_parse(headers) cookie = Base.json_parse(cookies) url_params = Base.joint_url(pre_case[data_key.params], pre_case[data_key.url]) res = self.run_api(url, method, url_params, reference, params, header) print("前置用例执行:%s" % res) return res
def run_pre(self, pre_case): """ 前置条件运行 :param pre_case: :return: """ pass url = ConfigYaml().get_conf_url() + pre_case[data_key.url] method = pre_case[data_key.method] params = pre_case[data_key.params] headers = pre_case[data_key.headers] cookies = pre_case[data_key.cookies] header = Base.json_parse(headers) cookie = Base.json_parse(cookies) res = self.run_api(url, method, params, header) print("前置用例执行: %s" % res) return res
def run_pre(self, pre_case): url = ConfigYaml().get_conf_url() + pre_case[data_key.url] method = pre_case[data_key.method] params = pre_case[data_key.params] headers = pre_case[data_key.headers] cookies = pre_case[data_key.cookies] # 判断headers是否存在,json转义 # if headers: # header = json.loads(headers) # else: # header = headers header = Base.json_parse(headers) # 增加cookies # if cookies: # cookie = json.loads(cookies) # else: # cookie = cookies cookie = Base.json_parse(cookies) res = self.run_api(url, method, params, header, cookie) print("前置用例执行:%s" % res) return res
有${}$则处理,无则原数据返回 """ url, headers, cookies, params, verif_data_pre, verif_db = logic( res_more, case_id=case_id, url=url, headers=headers, cookies=cookies, params=params) """ 对接口返回正确的数据做处理,组织当前接口的数据 1. str格式转换为dict,接口执行,并获取该接口返回数据 2. 对当前接口返回的部分数据做allure展示使用 3. 对期望的结果,包含返回数据及数据库期望数据放在r中一并返回,做断言准备 """ header = Base.json_parse(headers) cookie = Base.json_parse(cookies) param = Base.json_parse(params) # 执行当前用例 r = run_api(url, method, params_type, header, cookie, param) # 组织请求参数,用于之后的allure描述展示 r["requests_url"] = url r["requests_headers"] = header r["requests_cookies"] = cookie r["requests_params"] = param # 将需要验证的数据放在响应结果里边,用于之后的断言 r["verif_data_pre"] = verif_data_pre if verif_data_pre else None r["verif_db"] = verif_db if verif_db else None return r # 返回最终preA的结果
def test_run(self, case): # print("-----------------",case) # 重构函数内容 # data_key = ExcelConfig.DataConfig #run_list 第1个用例,key获取values url = ConfigYaml().get_conf_url() + case[data_key.url] print("----------", url) case_id = case[data_key.case_id] case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cooikes = case[data_key.cookies] code = case[data_key.code] db_verify = case[data_key.db_verify] #接口请求 #验证前置条件: if pre_exec: pass #找到执行用例 # 前置测试用例 pre_case = data_init.get_case_pre() print("前置条件信息为%s" % pre_exec) pre_res = self.run_pre(pre_case) headers, cooikes = self.get_correlation(headers, cooikes, pre_res) header = Base.json_parse(headers) cooikes = Base.json_parse(cooikes) res = self.run_api(url, method, params, headers, cooikes) print("测试用例执行:%s" % res) # #接口请求 # request = Request() # #params转义json # if len(str(params).strip()) is not 0: # params = json.loads(params) # #method post、get # if str(method).lower() == "get": # #增加了headers # res = request.get(url, json=params,headers = header,cooikes=cookie) # elif str(method).lower() == "post": # res = request.post(url, json=params,headers = header,cooikes=cookie) # else: # log.error("错误的请求method %s" % method) # print(res) #allure #sheet名称 feature一级标签 allure.dynamic.feature(sheet_name) #模块 story 二级标签 allure.dynamic.story(case_model) #用例ID+接口名称 title allure.dynamic.title(case_id + case_name) #请求url 请求类型 期望结果 实际结果描述 desc = "<font color = 'red'>请求URL:</font>{}<br/>" \ "<font color = 'red'>请求类型:</font>{}<br/>" \ "<font color = 'red'>期望结构:</font>{}<br/>" \ "<font color = 'red'>实际结果:</font>{}".format(url,method,expect_result,res) allure.dynamic.description(desc) # TestExcel().test_run() #断言验证 #状态码 assert_util = AssertUtil() assert_util.assert_code(int(res["code"]), int(code)) #返回结果内容 assert_util.assert_in_body(res["body"], str(expect_result)) #数据库结果断言 #初始化数据库 Base.assert_db("db_1", res("body"), db_verify) from common.Base import init_db
def test_run(self, case): # excel中取得用例数据 case_num = case[data_key.case_num] host = case[data_key.host] case_id = case[data_key.case_id] case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] note = case[data_key.note] params_type = case[data_key.params_type] params = case[data_key.params] body = case[data_key.body] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cookies = case[data_key.cookies] code = case[data_key.code] last_result = case[data_key.last_result] exclude_regex_paths = case[data_key.exclude_regex_paths] db_verify = case[data_key.db_verify] # 如果是预设的常用参数就替换下 if headers in global_params.keys(): headers = global_params[headers] if host in global_params.keys(): host = global_params[host] url = host + case[data_key.url] # 如果有参数中有需要执行前置用例的,就执行前置用例并替换参数 if pre_exec: pre_case = Data_init.get_case_pre(pre_exec) print("前置条件信息为:%s" % pre_case) pre_res = self.run_pre(pre_case) headers, cookies, params, body, url \ = Base.get_pre_exec_correlation(headers, cookies, params, body, url, pre_res) headers = Base.json_parse(headers) cookies = Base.json_parse(cookies) params = Base.json_parse(params) body = Base.json_parse(body) # 上面把数据都准备完成了,真正发请求 res = Base.run_api(url, method, params, body, headers, cookies) # allure报告信息 allure.dynamic.feature(sheet_name_keys) allure.dynamic.story(case_model) allure.dynamic.title(case_id + case_name) desc = "<font color='red'>请求URL:</font>{0}<BR/>" \ "<font color='red'>请求方法:</font>{1}<BR/>" \ "<font color='red'>预期结果:</font>{2}<BR/>" \ "<font color='red'>实际结果:</font>{3}".format(url, method, expect_result, res) allure.dynamic.description(desc) # log记录 log.info("=====接口【%s】的用例【%s%s】已执行!=====" % (case_name, case_id, note)) log.debug("\n测试用例预期:%s \n测试用例执行结果:%s" % (str(expect_result), str(res))) # 响应状态码及响应体字符串包含断言 assert_util = AssertUtil() assert_util.assert_code(res["code"], code) assert_util.assert_in_body(str(res["body"]), str(expect_result)) # 响应体的diff断言 if exclude_regex_paths != "skip_diff": if last_result: expect_result_for_diff = json.loads( json.dumps(eval(last_result))) if len(exclude_regex_paths) > 5: exclude_regex_paths = eval(exclude_regex_paths) else: exclude_regex_paths = None assert_util.assert_diff( expect_result_for_diff, res, exclude_regex_paths=exclude_regex_paths) else: from utils.ExcelUtil import ExcelUtil sheet_by = getattr(Const.GetConst(), (case_num[0:1]).upper()) excel_util = ExcelUtil(excel_file=case_file, sheet_by=sheet_by) case_row = excel_util.get_row_num_by_case_num(case_num) excel_util.write_back(case_row, 16, str(res)) # 数据库断言 if db_verify: assert_util.assert_db(db_verify)
def test_run(self, case): #run_list 第一个用例,用例,key获取values url = ConfigYaml().get_conf_url() + case[data_key.url] print(url) case_id = case[data_key.case_id] # print(case_id) case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cookies = case[data_key.cookies] code = case[data_key.code] db_verify = case[data_key.db_verify] #动态headers请求 ##1.验证前置条件 if pre_exec: pass ##2.找到执行用例 pre_case = data_init.get_case_pre(pre_exec) print("前置条件信息为: %s" % pre_case) pre_res = self.run_pre(pre_case) headers, cookies = self.get_correlation(headers, cookies, pre_res) header = Base.json_parse(headers) cookie = Base.json_parse(cookies) res = self.run_api(url, method, params, header, cookie) print("测试用例执行: %s" % res) # return res # 注意 # sheet名称--feature 一级标签 allure.dynamic.feature(sheet_name) # 模块--story 二级标签 allure.dynamic.story(case_model) # 用例ID + 接口名称 - - title allure.dynamic.title(case_id + case_name) #请求URL 请求类型 期望结果 实际结果 description # allure.dynamic.description(url+method+expect_result+res) desc = "<font color='green' size=3>请求URL: </font> {}<Br/>" \ "<font color='green' size=3>请求类型: </font>{}<Br/>" \ "<font color='red' size=3>期望结果: </font>{}<Br/>" \ "<font color='green' size=3>实际结果: </font>{}".format(url, method, expect_result, res) allure.dynamic.description(desc) #1.断言验证 #2.状态码,返回结果内容,数据库相关结果验证 #"""断言验证--状态码,返回结果内容,数据库相关的结果的验证""" #1.)状态码 assert_util = AssertUtil() assert_util.assert_code(int(res["code"]), int(code)) #2.)返回结果内容验证 assert_util.assert_in_body(str(res["body"]), str(expect_result)) #********数据库结果断言******** Base.assert_db("db_1", res["body"], db_verify)
def test_run(self, case): # data_key = ExcelConfig.DataConfig # run_list第一个用例 url = ConfigYaml().get_conf_url() + case[data_key.url] # print(url) case_id = case[data_key.case_id] case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cookies = case[data_key.cookies] code = case[data_key.code] db_verify = case[data_key.db_verify] # # 判断headers是否存在,json转义 # if headers: # header = json.loads(headers) # else: # header = headers # # 增加cookies # if cookies: # cookie = json.loads(cookies) # else: # cookie = cookies # 验证前置条件 if pre_exec: # 执行测试用例 pre_case = data_init.get_case_pre(pre_exec) # print("前置条件为:%s"%pre_case) pre_res = self.run_pre(pre_case) headers, cookies = self.get_correlation(headers, cookies, pre_res) header = Base.json_parse(headers) cookie = Base.json_parse(cookies) res = self.run_api(url, method, params, header, cookie) print("测试用例执行:%s" % res) # allure ''' sheet名称 feature 一级标签 模块 story 二级标签 用例ID+接口名称 title 请求url、请求类型、期望结果、实际结果 描述 ''' allure.dynamic.feature(sheet_name) allure.dynamic.story(case_model) allure.dynamic.title(case_id + case_name) desc = "<font color='red'> 请求URL:</font> {} <Br/> " \ "<font color='red'> 请求类型:</font> {} <Br/> " \ "<font color='red'> 期望结果:</font> {} <Br/> " \ "<font color='red'> 实际结果:</font> {} ".format(url,method,expect_result,res) allure.dynamic.description(desc) # 断言验证 # 状态码,返回结果内容,数据库相关结果验证 assert_util = AssertUtil() assert_util.assert_code((res['code']), int(code)) # 返回结果内容,body assert_util.assert_in_body(str(res["body"]), str(expect_result)) # 数据库断言 Base.assert_db("db_1", res['body'], db_verify)