API自动化工具之用例读取

项目GitHub地址:GitHub - muzili0903/APIframework

如果有疑问欢迎留言,当然如果觉得写得不错可以收藏或推荐一下,可以的话,麻烦GitHub帮忙给个小星星!!!

        此框架是基于Python+Pytest+Requests+Allure+Yaml+Json实现接口业务关联自动化测试,使用本框架,你无须编写任何代码,只需要你抓取接口数据包即可。关于框架的使用以及已实现的内容可以查阅该框架的首文介绍:API自动化工具雏形_YiHong_Li的博客-CSDN博客

代码展示:(注:框架运行进行用例读取,项目案例需要按照框架已圈定的格式进行编写)

用例读取返回格式如下:

[{"场景文件名1": 
    ["场景名": 
        [{"接口名1": 
            {"步骤名1": 
                {"脚本": 
                    {"请求头": {}, "请求体": {}, "请求数据": {}, "检查体": {}}}
                }
            }, 
            {"接口名2": 
            {"步骤名2": 
                {"脚本": 
                    {"请求头": {}, "请求体": {}, "请求数据": {}, "检查体": {}}}
                }
            }]
        ]
    },
{"场景文件名2": 
    ["场景名": 
        [{"接口名1": 
            {"步骤名1": 
                {"脚本": 
                    {"请求头": {}, "请求体": {}, "请求数据": {}, "检查体": {}}}
                }
            }, 
            {"接口名2": 
            {"步骤名2": 
                {"脚本": 
                    {"请求头": {}, "请求体": {}, "请求数据": {}, "检查体": {}}}
                }
            }]
        ]
    }]

1、获取场景用例,返回场景用例数据

def get_scene(path):
    """
    获取场景用例
    :param path:
    :return:
    """
    scene_all_file = get_all_file(path)
    scene = list()
    for scene_file in scene_all_file:
        scene_file_name = get_file_name(scene_file)
        scene_file = Config(scene_file)
        scene_name = scene_file.get_sections()
        scene_api = dict()
        for name in scene_name:
            scene_api_name = scene_file.get_items(name)
            scene_api_script = api_to_script(scene_file_name, scene_api_name)
            scene_api.update({name: scene_api_script})
        scene.append({scene_file_name: scene_api})
    return scene

2、获取脚本文件内容

def api_to_script(scene_file_name, scene_api_name):
    """
    替换接口名对应的接口script脚本
    :param scene_file_name: 场景文件名
    :param scene_api_name: 场景下的接口名
    :return:
    """
    temp = list()
    for api_name in scene_api_name:
        api_name_list = list(api_name)
        api_name_list[1] = get_script(api_name[1])
        api_data = get_case_data(scene_file_name, api_name[1])
        api_name_list[1].update({'data': api_data})
        # {接口名: {步骤: 接口脚本内容}}
        temp.append({api_name[1]: {api_name_list[0]: api_name_list[1]}})
    return temp


def get_script(file):
    """
    获取接口脚本文件内容
    :param file:
    :return:
    """
    path = APISCRIPT + '\\' + file + '.yaml'
    yaml_content = read_yaml(path, is_str=False)
    if yaml_content is not None:
        try:
            request_header = yaml_content.__getitem__('request_header')
            # 修改request_body从yaml目录下获取
            content = yaml_content.__getitem__('request_body')
            file_name = content.__getitem__('parameter')
            if '.yaml' in file_name:
                file_path = APIYAML + '\\' + file_name
            else:
                file_path = APIYAML + '\\' + file_name + '.yaml'
            request_body = read_yaml(file_path, is_str=False)
            check_body = content.pop('check_body')
            return {'script': {"request_header": request_header, "request_body": request_body, "check_body": check_body}}
        except Exception as e:
            logging.error("script脚本格式有误: >>>{}".format(file))
            logging.error("报错详情内容: >>>{}".format(e))
    return None

3、获取脚本对应用户参数化的数据

def get_case_data(scene_file, api_name):
    """
    获取用户自定义参数化变量值
    :param scene_file:
    :param api_name:
    :return:
    """
    path = APIDATA + '\\' + scene_file
    scene_file_data = Config(path)
    try:
        return dict(scene_file_data.get_items(api_name))
    except Exception as e:
        logging.error("场景文件对应的接口section不存在: >>>{}".format(scene_file))
        logging.error("报错详情内容: >>>{}".format(e))
        return None

运行结果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值