Postman 完全指南:API 测试从入门到精通
Postman 是最流行的 API 测试工具。本文介绍 Postman 的使用方法。
基本使用
发送请求
- 选择方法(GET/POST/PUT/DELETE)
- 输入 URL
- 设置 Headers
- 设置 Body
- 点击 Send
环境变量
{{baseUrl}}/api/users
{{token}}
测试脚本
// 验证状态码
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
// 验证响应体
pm.test("Response has name", function() {
var jsonData = pm.response.json();
pm.expect(jsonData.name).to.eql("张三");
});
集合测试
- 创建集合
- 添加请求
- 编写测试脚本
- 运行集合
自动化
# 命令行运行
newman run collection.json -e environment.json
最佳实践
- 使用环境变量:便于切换环境
- 编写测试脚本:自动化验证
- 使用集合:组织相关请求
- 文档化:添加描述和示例
总结
Postman 是强大的 API 测试工具。掌握基本使用和测试脚本,可以高效地测试 API。