Pytest自动化测试框架入门?
2025年b站最牛最全的postman接口测试及postman接口自动化测试全套教程从入门到精通!
1. 基础准备(5分钟)
-
安装 Postman:
-
示例接口:找一个免费测试 API
2. 核心四步法(50分钟)
① 发送请求(10分钟)
-
在 Postman 中新建请求,输入 URL(如
GET https://reqres.in/api/users/2
) -
点击 Send,查看返回的 JSON 数据。
② 写断言脚本(15分钟)
-
切换到 Tests 标签页,用 JavaScript 写断言:
javascript
// 检查状态码 pm.test("Status is 200", () => pm.response.to.have.status(200));// 检查响应数据 pm.test("User ID is correct", () => {const json = pm.response.json();pm.expect(json.data.id).to.eql(2); });
③ 参数化测试(15分钟)
-
环境变量:在 Tests 脚本中动态存储/读取变量:
javascript
// 存储变量 pm.environment.set("token", pm.response.json().token);// 使用变量(在请求URL或Body中用 {{token}})
-
CSV 数据驱动:
-
创建 CSV 文件(如
users.csv
):
csv
user_id,expected_name 1,George 2,Janet
-
在 Collection Runner 中上传 CSV,脚本中读取数据:
javascript
const userId = pm.iterationData.get("user_id"); pm.test(`Check name for user ${userId}`, () => {pm.expect(pm.response.json().data.first_name).to.eql(pm.iterationData.get("expected_name")); });
-
④ 批量运行(10分钟)
-
Collection Runner:选中集合 → 点击 Run → 设置迭代次数/上传数据文件 → 查看测试结果。
-
命令行执行(选学):导出集合后用 Newman 运行:
bash
npm install -g newman newman run collection.json