def _func(*args, **kwargs): r= func(*args, **kwargs) request = "---------------请求-----------------\n{0}\n{1}\n{2}".format(r.url,dic_to_str(r.request.headers),r.request.body) MyLog.info(request) response = "---------------响应----------------\n{0}\n{1}\n{2}".format(r.status_code,dic_to_str(r.headers),r.text) MyLog.info(response) allure.attach(request,'请求',allure.attachment_type.TEXT) allure.attach(response, '响应', allure.attachment_type.TEXT) return r
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 send_request(data, host, address, _path, relevance=None): """ 封装请求 :param data: 测试用例 :param host: 测试host :param address: 接口地址 :param relevance: 关联对象 :param _path: case路径 :return: """ logging.info("=" * 100) header = readParameter.read_param(data["test_name"], data["headers"], _path, relevance) if data["cookies"] is True: header["Cookie"] = initializeCookie.ini_cookie() logging.debug("请求头处理结果:%s" % header) parameter = readParameter.read_param(data["test_name"], data["parameter"], _path, relevance) logging.debug("请求参数处理结果:%s" % parameter) try: host = data["host"] except KeyError: pass try: address = data["address"] except KeyError: pass host = confManage.host_manage(host) address = replaceRelevance.replace(address, relevance) logging.debug("host处理结果: %s" % host) if not host: raise Exception("接口请求地址为空 %s" % data["headers"]) logging.info("请求接口:%s" % str(data["test_name"])) logging.info("请求地址:%s" % data["http_type"] + "://" + host + address) logging.info("请求头: %s" % str(header)) logging.info("请求参数: %s" % str(parameter)) if data["test_name"] == 'password正确': with allure.step("保存cookie信息"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) apiMethod.save_cookie(header=header, address=data["http_type"] + "://" + host + address, data=parameter) if data["request_type"].lower() == 'post': logging.info("请求方法: POST") if data["file"]: with allure.step("POST上传文件"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) result = apiMethod.post( header=header, address=data["http_type"] + "://" + host + address, request_parameter_type=data["parameter_type"], files=parameter, timeout=data["timeout"]) else: with allure.step("POST请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) result = apiMethod.post( header=header, address=data["http_type"] + "://" + host + address, request_parameter_type=data["parameter_type"], data=parameter, timeout=data["timeout"]) elif data["request_type"].lower() == 'get': with allure.step("GET请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("请求方法: GET") result = apiMethod.get(header=header, address=data["http_type"] + "://" + host + address, data=parameter, timeout=data["timeout"]) elif data["request_type"].lower() == 'put': logging.info("请求方法: PUT") if data["file"]: with allure.step("PUT上传文件"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) result = apiMethod.post( header=header, address=data["http_type"] + "://" + host + address, request_parameter_type=data["parameter_type"], files=parameter, timeout=data["timeout"]) else: with allure.step("PUT请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) result = apiMethod.post( header=header, address=data["http_type"] + "://" + host + address, request_parameter_type=data["parameter_type"], data=parameter, timeout=data["timeout"]) elif data["request_type"].lower() == 'delete': with allure.step("DELETE请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("请求方法: DELETE") result = apiMethod.get(header=header, address=data["http_type"] + "://" + host + address, data=parameter, timeout=data["timeout"]) else: result = {"code": False, "data": False} logging.info("请求接口结果:\n %s" % str(result)) return result
def send_request(data, host, address,port ,relevance, _path, success): """ 再次封装请求 :param data: 测试用例 :param host: 测试地址 :param address: 接口地址 :param relevance: 关联对象 :param port: 端口地址 :param _path: case路径 :param success: 全局结果 :return: """ # logging = Log.MyLog() config = Config.Config() logging.info("="*100) header = ReadParam.read_param(data["test_name"], data["headers"], relevance, _path, success) # 处理请求头 logging.debug("请求头处理结果: %s" % header) parameter = ReadParam.read_param(data["test_name"], data["parameter"], relevance, _path, success) # 处理请求参数 logging.debug("请求参数处理结果: %s" % header) try: # 如果用例中写了host和address,则使用用例中的host和address,若没有则使用全局的 host = data["host"] except KeyError: pass try: address = data["address"] except KeyError: pass host = config.host # host处理,读取配置文件中的host address = ParamManage.manage(address, relevance) logging.debug("host处理结果: %s" % host) if not host: raise failureException("接口请求地址为空 %s" % data["headers"]) logging.info("请求接口:%s" % str(data["test_name"])) logging.info("请求地址:%s" % data["http_type"] + "://" + host + ":"+ port+address) logging.info("请求头: %s" % str(header)) logging.info("请求参数: %s" % str(parameter)) if data["request_type"].lower() == 'post': if data["file"]: with allure.step("POST上传文件"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + ":"+ port+address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) result = confighttp.post(header=header, address=data["http_type"] + "://" + host + ":"+ port+address, request_parameter_type=data["parameter_type"], files=parameter, timeout=data["timeout"]) else: with allure.step("POST请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + ":"+ port+address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("POST请求接口") result = confighttp.post(header=header, address=data["http_type"] + "://" + host + ":"+ port+address, request_parameter_type=data["parameter_type"], data=parameter, timeout=data["timeout"]) elif data["request_type"].lower() == 'get': with allure.step("GET请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + ":"+ port+address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("GET请求接口") result = confighttp.get(header=header, address=data["http_type"] + "://" + host + ":"+ port+address, data=parameter, timeout=data["timeout"]) elif data["request_type"].lower() == "put": if data["file"]: with allure.step("PUT上传文件"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + ":"+ port+address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("PUT上传文件") result = confighttp.put(header=header, address=data["http_type"] + "://" + host + ":"+ port+address, request_parameter_type=data["parameter_type"], files=parameter, timeout=data["timeout"]) else: with allure.step("PUT请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + ":"+ port+address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("PUT请求接口") result = confighttp.put(header=header, address=data["http_type"] + "://" + host + ":"+ port+address, request_parameter_type=data["parameter_type"], data=parameter, timeout=data["timeout"]) elif data["request_type"].lower() == "delete": with allure.step("DELETE请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + ":"+ port+address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("DELETE请求接口") result = confighttp.delete(header=header, address=data["http_type"] + "://" + host + ":"+ port+address, data=parameter, timeout=data["timeout"]) else: result = {"code": False, "data": False} logging.info("接口请求结果:\n %s" % str(result)) return result
def __init__(self): logging.info("初始化关联文件") self.data = ConfRelevance(CONF_PATH, "env").get_relevance_conf()