def check_json(src_data, dst_data, success): """ 校验的json :param src_data: 校验内容 :param dst_data: 接口返回的数据(被校验的内容) :param success: 全局测试结果 :return: """ global result if isinstance(src_data, dict): # 若为dict格式 for key in src_data: if key not in dst_data: success["result"] = False raise failureException("JSON格式校验,关键字 %s 不在返回结果 %s" % (key, dst_data)) elif type(src_data[key] ) is not dict and src_data[key] != dst_data[key]: value = src_data[key] success["result"] = False raise failureException("JSON格式校验,关键字 %s 不在返回结果 %s" % (value, dst_data)) else: this_key = key # 递归 if isinstance(src_data[this_key], dict) and isinstance( dst_data[this_key], dict): check_json(src_data[this_key], dst_data[this_key], success) elif isinstance(type(src_data[this_key]), type(dst_data[this_key])): success["result"] = False raise failureException( "JSON格式校验,关键字 %s 与 %s 类型不符" % (src_data[this_key], dst_data[this_key])) else: pass else: success["result"] = False raise failureException("JSON校验内容非dict格式")
def read_json(test_name, code_json, relevance, _path, result): """ 校验内容读取 :param test_name: 用例名称,用作索引 :param code_json: 文件路径 :param relevance: 关联对象 :param _path: case路径 :param result: 全局结果 :return: """ # 用例中参数为json格式 if isinstance(code_json, dict): code_json = manage(code_json, relevance) # 用例中参数非json格式 else: try: with open(_path + "/" + code_json, "r", encoding="utf-8") as f: data = json.load(f) for i in data: # 遍历,通过用例名称做索引查找到第一个期望结果后,跳出循环 if i["test_name"] == test_name: code_json = i["json"] break # 如果code_json为空,表示未找到用例关联的期望结果 if not code_json: result["result"] = False raise failureException("未找到用例关联的期望结果\n文件路径: %s\n索引: %s" % (code_json, test_name)) else: code_json = manage(code_json, relevance) # code_json = dict(code_json) # 文件不存在 except FileNotFoundError: result["result"] = False raise failureException("用例关联文件不存在\n文件路径: %s" % code_json) # 文件存在,但里面存储的数据有误,json.load执行异常 except JSONDecodeError: result["result"] = False raise failureException("用例关联的期望文件有误\n文件路径: %s" % code_json) # 返回获取的期望结果 return code_json[0]
def ini_request(case_dict, relevance, _path, result): """ 用例前提条件执行,提取关联键 :param case_dict: 用例对象 :param relevance: 关联对象 :param _path: case路径 :param result: 全局结果 :return: """ if isinstance(case_dict["premise"], list): logging.info("执行测试用例前置接口") with allure.step("接口关联请求"): for i in case_dict["premise"]: relevance_list = relevance.copy() for j in range(0, 3): # 获取前置接口关联数据失败 code, data = send_request( i, case_dict["testinfo"].get("host"), case_dict["testinfo"].get("address"), relevance_list, _path, result) if not data: with allure.step("接口请求失败!等待三秒后重试!"): pass logging.info("接口请求失败!等待三秒后重试!") continue if i["relevance"]: if len(i["relevance"]): relevance = get_relevance(data, i["relevance"], relevance) if isinstance(relevance, bool): with allure.step("从结果中提取关联键的值失败!等待3秒后重试!"): pass logging.info("从结果中提取关联键的值失败!等待3秒后重试!") time.sleep(3) continue else: break else: break else: break if isinstance(relevance, bool): logging.info("从结果中提取关联键的值失败!重试三次失败") result["result"] = False raise failureException("获取前置接口关联数据失败") return relevance
def check(test_name, case_data, code, data, relevance, _path, success): """ 校验测试结果 :param test_name: 测试用例 :param case_data: 测试用例 :param code: HTTP状态 :param data: 返回的接口json数据 :param relevance: 关联值对象 :param _path: case路径 :param success: 全局测试结果 :return: """ # 不校验 if case_data["check_type"] == 'no_check': with allure.step("不校验结果"): pass # 校验json格式 elif case_data["check_type"] == 'json': expected_request = case_data["expected_request"] # 判断预期结果格式,如果是字符串,则打开文件路径,提取保存在文件中的期望结果 if isinstance(case_data["expected_request"], str): expected_request = expected_manage.read_json( test_name, expected_request, relevance, _path, success) with allure.step("JSON格式校验"): allure.attach("期望code", str(case_data["expected_code"])) allure.attach('期望data', str(expected_request)) allure.attach("实际code", str(code)) allure.attach('实际data', str(data)) if int(code) == case_data["expected_code"]: if not data: data = "{}" # json校验 check_json.check_json(expected_request, data, success) else: success["result"] = False if case_data.get("CustomFail"): info = custom_fail.custom_manage(case_data.get("CustomFail"), relevance) raise failureException( str(info) + "\nhttp状态码错误!\n %s != %s" % (code, case_data["expected_code"])) else: raise failureException("http状态码错误!\n %s != %s" % (code, case_data["expected_code"])) # 只校验HTTP状态 elif case_data["check_type"] == 'only_check_status': with allure.step("校验HTTP状态"): allure.attach("期望code", str(case_data["expected_code"])) allure.attach("实际code", str(code)) if int(code) == case_data["expected_code"]: pass else: success["result"] = False if case_data.get("CustomFail"): info = custom_fail.custom_manage(case_data.get("CustomFail"), relevance) raise failureException( str(info) + "\nhttp状态码错误!\n %s != %s" % (code, case_data["expected_code"])) else: raise failureException("http状态码错误!\n %s != %s" % (code, case_data["expected_code"])) # 完全校验 elif case_data["check_type"] == 'entirely_check': expected_request = case_data["expected_request"] # 判断预期结果格式,如果是字符串,则打开文件路径,提取保存在文件中的期望结果 if isinstance(case_data["expected_request"], str): expected_request = expected_manage.read_json( test_name, expected_request, relevance, _path, success) with allure.step("完全校验"): allure.attach("期望code", str(case_data["expected_code"])) allure.attach('期望data', str(expected_request)) allure.attach("实际code", str(code)) allure.attach('实际data', str(data)) if int(code) == case_data["expected_code"]: result = operator.eq(expected_request, data) if result: pass else: success["result"] = False if case_data.get("CustomFail"): info = custom_fail.custom_manage( case_data.get("CustomFail"), relevance) raise failureException( str(info) + "\n完全校验失败! %s ! = %s" % (expected_request, data)) else: raise failureException("完全校验失败! %s ! = %s" % (expected_request, data)) else: success["result"] = False raise failureException("http状态码错误!\n %s != %s" % (code, case_data["expected_code"])) # 正则校验 elif case_data["check_type"] == 'Regular_check': if int(code) == case_data["expected_code"]: try: result = "" # 初始化校验内容 if isinstance(case_data["expected_request"], list): # 多个正则表达式校验,遍历校验 for i in case_data["expected_request"]: result = re.findall(i.replace("\"", "\'"), str(data)) allure.attach('校验完成结果\n', str(result)) else: # 单个正则表达式 result = re.findall( case_data["expected_request"].replace("\"", "\'"), str(data)) with allure.step("正则校验"): allure.attach("期望code", str(case_data["expected_code"])) allure.attach( '正则表达式', str(case_data["expected_request"]).replace( "\'", "\"")) allure.attach("实际code", str(code)) allure.attach('实际data', str(data)) allure.attach( case_data["expected_request"].replace("\"", "\'") + '校验完成结果', str(result).replace("\'", "\"")) # 未匹配到校验内容 if not result: success["result"] = False if case_data.get("CustomFail"): info = custom_fail.custom_manage( case_data.get("CustomFail"), relevance) raise failureException( str(info) + "\n正则未校验到内容! %s" % case_data["expected_request"]) else: raise failureException("正则未校验到内容! %s" % case_data["expected_request"]) # 正则表达式为空时 except KeyError: success["result"] = False raise failureException("正则校验执行失败! %s\n正则表达式为空时" % case_data["expected_request"]) else: success["result"] = False raise failureException("http状态码错误!\n %s != %s" % (code, case_data["expected_code"])) # 数据库校验 elif case_data["check_type"] == "datebase_check": pass else: success["result"] = False raise failureException("无该校验方式%s" % case_data["check_type"])