# pytest_interface_project **Repository Path**: hu-ruifan/pytest_interface_project ## Basic Information - **Project Name**: pytest_interface_project - **Description**: pytest框架接口自动化测试 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2023-11-05 - **Last Updated**: 2025-08-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### api_framework ###### **简介** 该框架只需要通过yaml文件,即可完成接口自动化测试的框架,既降低了接口自动化的难度,同时也为迁移接口自动化测试平台打下了基础。 ###### **技术特点** 本框架使用Python+pytest+requests+yaml+jsonpath+allure等组件来搭建,实现了: 1、yaml文件自动读取 2、yaml文件动态输入 3、yaml文件内容替换 4、数据驱动测试 5、接口关联(状态码、响应头、响应正文、cookies、url等) 6、热加载函数(类型转换、加密解密、数据库查询、长度校验等) 7、日志记录 8、html报告 9、其他 ###### **环境搭建** 1、安装Python 3.10 2、下载框架源码 3、安装环境依赖 pip install -r requirement.txt 4、启动框架 python run.py 5、查看报告 报告路径:report/index.html ###### **框架使用** 1、创建测试项目 创建目录,将创建好的测试目录写到setting.py中的case_path中,例如:case_path = "testcase_yaml" 2、创建测试文件 测试文件的名字要求: 1️⃣ 以test_ 开头 2️⃣ 文件以名字排序,并决定执行顺序 3️⃣ 测试用例文件以 .yaml 后缀 3、编写测试用例 yaml文件必填字段 | 字段名 | 用途 | 备注 | | -------- | -------- | -------- | | feature | 模块名称 | | | story | 接口名称 | | | title | 用例名称 | | | request | 请求信息 | | | extract | 提取信息 | 若没有提取信息,设置data:null | | validate | 断言信息 | 断言定义在caseinfo中 | 选填字段 | 字段名 | 用途 | 备注 | | ----------- | -------------------- | ---- | | parametrize | 参数化,进行数据驱动 | Text | 示例: feature: 用户模块 story: 登录接口 title: ${title} request: method: POST url: /portal/user/register headers: Content-Type: application/json json: "mobile": ${mobile} "uniqueId": "" "userType": 1 "validateCode": ${validateCode} extract: token: [json, $..token, 0] #使用jsonpath提取token,取list中的第一个数据 mainAccountId: [json, $..mainAccountId, 0] msg: [json, $..msg, 0] validate: equals: 响应code码验证: - ${assert_msg} # 预期结果 - ${msg} # 实际结果 parametrize: - ["title", "mobile", "validateCode", "assert_msg"] - ["手机号长度不正确", "185890431931", "123456", "手机号码长度不对"] - ["手机号为空", "", "123456", "手机号码不能为空"] - ["短信验证码为空","18589043193", "", "短信验证码不能为空"] - ["正常登陆", "18589043193", "123456", "成功"] 注: 1、如果接口需要用到上传文件,直接填写文件路径即可; 2、如果参数是null,直接写 null; 3、如果请求参数中需要用到热加载的方法,写法如下:${sql(select * from student)} request: method: post url: /portal/obsFile/fileUpload headers: AppBuildVersion: '1.5.5' Authorization: Bearer ${token} TerminalType: 'iOS' files: files: data/pic/3.jpg 4、执行用例 python run.py **联系方式:** 邮箱:1579118105@qq.com