From 4a46a052d4eba68a330205db75840c9478ab2306 Mon Sep 17 00:00:00 2001 From: zy7y <7631909+zy7y@user.noreply.gitee.com> Date: Mon, 10 Aug 2020 15:48:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86test=5Fapi.py=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E4=BE=9D=E8=B5=96=E6=95=B0=E6=8D=AE=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=8F=90=E5=87=BA=E6=9D=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/data_tearing.py | 87 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 tools/data_tearing.py diff --git a/tools/data_tearing.py b/tools/data_tearing.py new file mode 100644 index 0000000..3309ffb --- /dev/null +++ b/tools/data_tearing.py @@ -0,0 +1,87 @@ +#!/usr/bin/env/python3 +# -*- coding:utf-8 -*- +""" +@project: apiAutoTest +@author: zy7y +@file: data_tearing.py +@ide: PyCharm +@time: 2020/8/10 +""" +import json +from json import JSONDecodeError + +import jsonpath +from loguru import logger + + +class TreatingData(object): + """ + 处理hader/path路径参数/请求data依赖数据代码 + """ + + def __init__(self): + self.no_token_header = {} + self.token_header = {} + + def treating_data(self, is_token, parameters, dependent, data, save_response_dict): + # 使用那个header + if is_token == '': + header = self.no_token_header + else: + header = self.token_header + logger.info(f'处理依赖前data的数据:{data} \n') + # 处理依赖数据data + if dependent != '': + dependent_data = save_response_dict.read_depend_data(dependent) + logger.debug(f'依赖数据解析获得的字典{dependent_data}') + if data != '': + # 合并组成一个新的data + dependent_data.update(json.loads(data)) + data = dependent_data + logger.debug(f'data有数据,依赖有数据时 {data}') + else: + # 赋值给data + data = dependent_data + logger.debug(f'data无数据,依赖有数据时 {data}') + else: + if data == '': + data = None + logger.debug(f'data无数据,依赖无数据时 {data}') + else: + data = json.loads(data) + logger.debug(f'data有数据,依赖无数据 {data}') + + # 处理路径参数Path的依赖 + # 传进来的参数类似 {"case_002":"$.data.id"}/item/{"case_002":"$.meta.status"},进行列表拆分 + path_list = parameters.split('/') + # 获取列表长度迭代 + for i in range(len(path_list)): + # 按着 + try: + # 尝试序列化成dict: json.loads('2') 可以转换成2 + path_dict = json.loads(path_list[i]) + except JSONDecodeError as e: + # 序列化失败此path_list[i]的值不变化 + logger.debug(f'无法转换字典,进入下一个检查,本轮值不发生变化:{path_list[i]},\n {e}') + # 跳过进入下次循环 + continue + else: + # 解析该字典,获得用例编号,表达式 + logger.error(f'{path_dict}') + # 处理json.loads('数字')正常序列化导致的AttributeError + try: + for k, v in path_dict.items(): + try: + # 尝试从对应的case实际响应提取某个字段内容 + path_list[i] = jsonpath.jsonpath(save_response_dict.actual_response[k], v)[0] + except TypeError as e: + logger.info(f'无法提取,请检查响应字典中是否支持该表达式,{e}') + except AttributeError as e: + logger.error(f'类型错误:{type(path_list[i])},本此将不转换值 {path_list[i]},\n {e}') + # 字典中存在有不是str的元素:使用map 转换成全字符串的列表 + path_list = map(str, path_list) + + # 将字符串列表转换成字符:500/item/200 + parameters_path_url = "/".join(path_list) + logger.info(f'path路径参数解析依赖后的路径为{parameters_path_url}') + return data, header, parameters_path_url \ No newline at end of file