diff --git a/comm/unit/initializePremise.py b/comm/unit/initializePremise.py index c2a2289..9a9183d 100644 --- a/comm/unit/initializePremise.py +++ b/comm/unit/initializePremise.py @@ -37,6 +37,51 @@ def read_json(summary, json_obj, case_path): raise Exception("用例关联的文件有误\n文件路径: %s" % json_obj) +def prepare_case(pre_case_path, relevance): + """ + 调用前置接口并返回响应消息体 + :param pre_case_path: 前置用例 + :param relevance: 关联值对象 + :return: + """ + # 获取前置接口用例 + logging.info("获取前置接口测试用例:{}".format(pre_case_path)) + pre_case_yaml = PAGE_DIR + pre_case_path + pre_case_path = os.path.dirname(pre_case_yaml) + pre_case_dict = readYaml.read_yaml_data(pre_case_yaml) + pre_test_info = pre_case_dict['test_info'] + pre_case_data = pre_case_dict['test_case'][0] + # 判断前置接口是否也存在前置接口 + if pre_test_info["premise"]: + init_premise(pre_test_info, pre_case_data, pre_case_path) + + for i in range(3): + # 处理前置接口测试信息 + pre_test_info = replaceRelevance.replace(pre_test_info, relevance) + logging.debug("测试信息处理结果:{}".format(pre_test_info)) + # 处理前置接口Cookies + if pre_test_info['cookies']: + aconfig = readYaml.read_yaml_data(API_CONFIG) + cookies = aconfig[PROJECT_NAME]['cookies'] + logging.debug("请求Cookies处理结果:{}".format(cookies)) + # 处理前置接口入参:获取入参-替换关联值-发送请求 + pre_parameter = read_json(pre_case_data['summary'], pre_case_data['parameter'], pre_case_path) + pre_parameter = replaceRelevance.replace(pre_parameter, relevance) + pre_case_data['parameter'] = pre_parameter + logging.debug("请求参数处理结果:{}".format(pre_parameter)) + logging.info("执行前置接口测试用例:{}".format(pre_test_info)) + code, data = apiSend.send_request(pre_test_info, pre_case_data) + # 检查接口是否调用成功 + if data: + return data + else: + time.sleep(1) + logging.error("前置接口请求失败!等待1秒后重试!") + else: + logging.info("前置接口请求失败!尝试三次失败!") + raise Exception("获取前置接口关联数据失败!") + + def init_premise(test_info, case_data, case_path): """用例前提条件执行,提取关键值 @@ -57,58 +102,31 @@ def init_premise(test_info, case_data, case_path): logging.debug("请求Cookies处理结果:{}".format(cookies)) # 判断是否存在前置接口 - pre_case_path = test_info["premise"] - if pre_case_path: - # 获取前置接口用例 - logging.info("获取前置接口测试用例:{}".format(pre_case_path)) - pre_case_yaml = PAGE_DIR + pre_case_path - pre_case_path = os.path.dirname(pre_case_yaml) - pre_case_dict = readYaml.read_yaml_data(pre_case_yaml) - pre_test_info = pre_case_dict['test_info'] - pre_case_data = pre_case_dict['test_case'][0] - # 判断前置接口是否也存在前置接口 - if pre_test_info["premise"]: - init_premise(pre_test_info, pre_case_data, pre_case_path) - - for i in range(3): - # 处理前置接口测试信息 - pre_test_info = replaceRelevance.replace(pre_test_info, __relevance) - logging.debug("测试信息处理结果:{}".format(pre_test_info)) - # 处理前置接口Cookies - if pre_test_info['cookies']: - cookies = aconfig[PROJECT_NAME]['cookies'] - logging.debug("请求Cookies处理结果:{}".format(cookies)) - # 处理前置接口入参:获取入参-替换关联值-发送请求 - pre_parameter = read_json(pre_case_data['summary'], pre_case_data['parameter'], pre_case_path) - pre_parameter = replaceRelevance.replace(pre_parameter, __relevance) - pre_case_data['parameter'] = pre_parameter - logging.debug("请求参数处理结果:{}".format(pre_parameter)) - logging.info("执行前置接口测试用例:{}".format(pre_test_info)) - code, data = apiSend.send_request(pre_test_info, pre_case_data) - - # 检查接口是否调用成功 - if data: - # 处理当前接口入参:获取入参-获取关联值-替换关联值 - parameter = read_json(case_data['summary'], case_data['parameter'], case_path) - __relevance = readRelevance.get_relevance(data, parameter, __relevance) - parameter = replaceRelevance.replace(parameter, __relevance) - case_data['parameter'] = parameter - logging.debug("请求参数处理结果:{}".format(parameter)) - - # 获取当前接口期望结果:获取期望结果-获取关联值-替换关联值 - expected_rs = read_json(case_data['summary'], case_data['check_body']['expected_result'], case_path) - parameter['data'] = data - __relevance = readRelevance.get_relevance(parameter, expected_rs, __relevance) - expected_rs = replaceRelevance.replace(expected_rs, __relevance) - case_data['check_body']['expected_result'] = expected_rs - logging.debug("期望返回处理结果:{}".format(case_data)) - break - else: - time.sleep(1) - logging.error("前置接口请求失败!等待1秒后重试!") + pre_case_path_list = test_info["premise"] + if pre_case_path_list: + if isinstance(pre_case_path_list, list): + data = [] + for pre_case_path in pre_case_path_list: + each_data = prepare_case(pre_case_path, __relevance) + data.append(each_data) else: - logging.info("前置接口请求失败!尝试三次失败!") - raise Exception("获取前置接口关联数据失败!") + data = prepare_case(pre_case_path_list, __relevance) + + # 处理当前接口入参:获取入参-获取关联值-替换关联值 + parameter = read_json(case_data['summary'], case_data['parameter'], case_path) + __relevance = readRelevance.get_relevance(data, parameter, __relevance) + parameter = replaceRelevance.replace(parameter, __relevance) + case_data['parameter'] = parameter + logging.debug("请求参数处理结果:{}".format(parameter)) + + # 获取当前接口期望结果:获取期望结果-获取关联值-替换关联值 + expected_rs = read_json(case_data['summary'], case_data['check_body']['expected_result'], case_path) + parameter['data'] = data + __relevance = readRelevance.get_relevance(parameter, expected_rs, __relevance) + expected_rs = replaceRelevance.replace(expected_rs, __relevance) + case_data['check_body']['expected_result'] = expected_rs + logging.debug("期望返回处理结果:{}".format(case_data)) + else: # 处理当前接口入参:获取入参-获取关联值-替换关联值 parameter = read_json(case_data['summary'], case_data['parameter'], case_path)