🛠️MatrixTools
Postman API测试完整教程
返回教程列表

Postman API测试完整教程

从基础到高级,学习使用Postman进行API测试、自动化测试和文档生成

MatrixTools
2024年7月20日
精选教程

教程简介

从基础到高级,学习使用Postman进行API测试、自动化测试和文档生成

PostmanAPI测试自动化测试后端开发
📖

教程详细内容

深度解析每个关键概念,配合实际案例帮助理解

Postman API测试完整教程:从入门到自动化测试

Postman简介:现代API开发的必备工具

Postman是全球最受欢迎的API开发协作平台,被超过2000万开发者使用。它不仅是一个API测试工具,更是一个完整的API开发生态系统,支持API设计、测试、文档化、监控和协作。

为什么选择Postman?

核心优势

  • 直观的界面:无需编程背景即可使用
  • 全面的功能:涵盖API开发的全生命周期
  • 强大的测试能力:支持单元测试、集成测试、性能测试
  • 团队协作:工作空间、版本控制、权限管理
  • 丰富的集成:CI/CD、监控、文档等

应用场景

  • API功能测试和验证
  • 自动化测试套件构建
  • API性能监控
  • 团队协作和知识共享
  • API文档生成和维护

安装与基础设置

安装Postman

桌面版安装(推荐):

  1. 选择对应操作系统版本下载
  2. 运行安装程序,完成安装
  3. 创建免费账户或登录现有账户

Web版使用

界面布局详解

┌─────────────────────────────────────────────────────────┐
│ 菜单栏:File, View, Help                                │
├─────────────────────────────────────────────────────────┤
│ 工具栏:New, Import, Runner, Sync                       │
├──────────────┬──────────────────────────────────────────┤
│              │ 请求构建器 (Request Builder)              │
│   侧边栏     │ ┌─────────────────────────────────────┐  │
│ ┌──────────┐ │ │ HTTP方法 │ URL              │ Send │  │
│ │ 工作空间 │ │ ├─────────────────────────────────────┤  │
│ │ 集合     │ │ │ Params │ Auth │ Headers │ Body  │  │
│ │ 环境     │ │ └─────────────────────────────────────┘  │
│ │ 历史     │ │                                        │
│ └──────────┘ │ 响应区域 (Response)                     │
│              │ ┌─────────────────────────────────────┐  │
│              │ │ Body │ Cookies │ Headers │ Test    │  │
│              │ └─────────────────────────────────────┘  │
└──────────────┴──────────────────────────────────────────┘

基础HTTP请求操作

GET请求:获取数据

示例:获取用户信息

GET https://jsonplaceholder.typicode.com/users/1

Headers:
Content-Type: application/json
Accept: application/json

响应示例:
{
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "[email protected]",
  "address": {
    "street": "Kulas Light",
    "suite": "Apt. 556",
    "city": "Gwenborough",
    "zipcode": "92998-3874",
    "geo": {
      "lat": "-37.3159",
      "lng": "81.1496"
    }
  }
}

查询参数设置

URL: https://api.example.com/search
Params:
  - q: javascript
  - page: 1
  - limit: 10
  - sort: date

实际请求URL:
https://api.example.com/search?q=javascript&page=1&limit=10&sort=date

POST请求:创建数据

JSON数据提交

POST https://jsonplaceholder.typicode.com/users

Headers:
Content-Type: application/json

Body (raw JSON):
{
  "name": "John Doe",
  "username": "johndoe",
  "email": "[email protected]",
  "phone": "1-770-736-8031",
  "website": "johndoe.org"
}

表单数据提交

POST https://httpbin.org/post

Body (form-data):
- name: John Doe
- email: [email protected]
- avatar: [选择文件上传]

PUT/PATCH请求:更新数据

PUT(完整更新)

PUT https://jsonplaceholder.typicode.com/users/1

Body:
{
  "id": 1,
  "name": "Updated Name",
  "username": "updated_username",
  "email": "[email protected]",
  // 需要包含所有字段
}

PATCH(部分更新)

PATCH https://jsonplaceholder.typicode.com/users/1

Body:
{
  "name": "Partially Updated Name"
  // 只需要更新的字段
}

DELETE请求:删除数据

DELETE https://jsonplaceholder.typicode.com/users/1

Headers:
Authorization: Bearer your-auth-token

身份验证配置

API Key认证

Header方式

Headers:
X-API-Key: your-api-key-here

Query参数方式

URL参数:
api_key=your-api-key-here

Bearer Token认证

Headers:
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

在Postman中设置

  1. 选择Authorization标签
  2. Type选择"Bearer Token"
  3. 输入token值

Basic Auth认证

Headers:
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

自动编码设置

  1. Authorization标签 > Type: Basic Auth
  2. 输入Username和Password
  3. Postman自动进行Base64编码

OAuth 2.0认证

配置流程

  1. Authorization > Type: OAuth 2.0
  2. 配置参数:

环境变量和全局变量

环境管理

创建环境

  1. 点击右上角环境下拉菜单
  2. 选择"Manage Environments"
  3. 点击"Add"创建新环境

环境变量示例

// 开发环境
{
  "baseUrl": "https://dev-api.example.com",
  "apiKey": "dev-api-key-123",
  "userId": "dev-user-001"
}

// 生产环境
{
  "baseUrl": "https://api.example.com",
  "apiKey": "prod-api-key-456",
  "userId": "prod-user-001"
}

使用环境变量

URL: {{baseUrl}}/users/{{userId}}
Headers:
X-API-Key: {{apiKey}}

全局变量设置

脚本设置全局变量

// Pre-request Script 或 Tests 中
pm.globals.set("authToken", "abc123");
pm.globals.set("timestamp", Date.now());

// 获取变量
const token = pm.globals.get("authToken");

变量作用域优先级

局部变量 > 环境变量 > 全局变量 > 数据变量

编写测试脚本

基础断言测试

// 检查响应状态码
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

// 检查响应时间
pm.test("Response time is less than 500ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(500);
});

// 检查响应包含特定文本
pm.test("Response contains user name", function () {
    pm.expect(pm.response.text()).to.include("John Doe");
});

// 检查JSON响应
pm.test("User has correct email", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData.email).to.eql("[email protected]");
});

高级测试脚本

// 动态变量提取
pm.test("Extract and save user ID", function () {
    const jsonData = pm.response.json();
    pm.environment.set("extractedUserId", jsonData.id);
    console.log("Saved user ID:", jsonData.id);
});

// 数组验证
pm.test("Response has users array", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('users');
    pm.expect(jsonData.users).to.be.an('array');
    pm.expect(jsonData.users.length).to.be.above(0);
});

// 条件测试
pm.test("Conditional test based on environment", function () {
    if (pm.environment.get("environment") === "production") {
        pm.expect(pm.response.responseTime).to.be.below(200);
    } else {
        pm.expect(pm.response.responseTime).to.be.below(1000);
    }
});

// 循环验证
pm.test("All users have required fields", function () {
    const jsonData = pm.response.json();
    jsonData.users.forEach(function(user) {
        pm.expect(user).to.have.property('id');
        pm.expect(user).to.have.property('name');
        pm.expect(user).to.have.property('email');
        pm.expect(user.email).to.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/);
    });
});

Pre-request Scripts

// 生成动态数据
const randomEmail = `user${Date.now()}@example.com`;
pm.environment.set("randomEmail", randomEmail);

// 获取认证token
const authUrl = pm.environment.get("authUrl");
const credentials = {
    username: pm.environment.get("username"),
    password: pm.environment.get("password")
};

pm.sendRequest({
    url: authUrl,
    method: 'POST',
    header: {
        'Content-Type': 'application/json'
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify(credentials)
    }
}, function (err, response) {
    if (!err) {
        const jsonData = response.json();
        pm.environment.set("authToken", jsonData.token);
    }
});

集合管理和组织

集合创建和结构

集合结构示例

📁 E-commerce API Testing
├── 📁 Authentication
│   ├── 🔗 Login
│   ├── 🔗 Register
│   └── 🔗 Refresh Token
├── 📁 User Management
│   ├── 🔗 Get User Profile
│   ├── 🔗 Update Profile
│   └── 🔗 Delete Account
├── 📁 Product Catalog
│   ├── 🔗 List Products
│   ├── 🔗 Get Product Details
│   ├── 🔗 Create Product
│   ├── 🔗 Update Product
│   └── 🔗 Delete Product
└── 📁 Order Processing
    ├── 🔗 Create Order
    ├── 🔗 Get Order Status
    └── 🔗 Cancel Order

集合级别配置

Pre-request Script(集合级)

// 为整个集合设置通用头部
pm.request.headers.add({
    key: "User-Agent",
    value: "PostmanAPITesting/1.0"
});

// 设置通用认证
if (!pm.environment.get("authToken")) {
    // 执行登录流程
    console.log("No auth token found, skipping authenticated requests");
}

集合变量

// 在集合级别定义常量
{
    "apiVersion": "v1",
    "defaultLimit": "10",
    "maxRetries": "3"
}

自动化测试和运行器

Collection Runner使用

基础运行配置

  1. 点击集合旁的"Run"按钮
  2. 选择要运行的请求
  3. 设置迭代次数和延迟
  4. 选择环境和数据文件
  5. 点击"Start Run"

数据驱动测试

name,email,age
John Doe,[email protected],25
Jane Smith,[email protected],30
Bob Johnson,[email protected],35

在请求中使用CSV数据

{
    "name": "{{name}}",
    "email": "{{email}}",
    "age": {{age}}
}

Newman命令行工具

安装Newman

npm install -g newman

基础运行命令

# 运行本地集合文件
newman run collection.json -e environment.json

# 运行在线集合
newman run https://www.getpostman.com/collections/your-collection-id

# 生成HTML报告
newman run collection.json -r html --reporter-html-export report.html

# 集成到CI/CD
newman run collection.json --bail --reporters cli,junit --reporter-junit-export results.xml

高级Newman配置

# 使用数据文件
newman run collection.json -d data.csv -n 100

# 设置延迟和超时
newman run collection.json --delay-request 500 --timeout-request 10000

# 使用多个环境
newman run collection.json -e staging.json -e secrets.json

性能测试和监控

基础性能测试

// 性能基准测试
pm.test("Response time baseline", function () {
    pm.expect(pm.response.responseTime).to.be.below(1000);
});

// 内存泄漏检测
pm.test("Memory usage check", function () {
    const responseSize = pm.response.responseSize;
    pm.expect(responseSize).to.be.below(5000000); // 5MB limit
});

// 并发测试脚本
const numRequests = 10;
let completedRequests = 0;
let responseTimes = [];

for (let i = 0; i < numRequests; i++) {
    pm.sendRequest(pm.request, function(err, response) {
        completedRequests++;
        responseTimes.push(response.responseTime);

        if (completedRequests === numRequests) {
            const avgResponseTime = responseTimes.reduce((a, b) => a + b) / responseTimes.length;
            console.log("Average response time:", avgResponseTime);
            pm.test("Average response time acceptable", function() {
                pm.expect(avgResponseTime).to.be.below(500);
            });
        }
    });
}

Postman监控设置

创建监控

  1. 集合右侧菜单 > "Monitor Collection"
  2. 设置监控名称和频率
  3. 选择环境和地区
  4. 配置通知规则

监控配置示例

// 监控专用测试
pm.test("Service availability", function () {
    pm.response.to.have.status(200);
    pm.expect(pm.response.responseTime).to.be.below(2000);
});

pm.test("Critical endpoint health", function () {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('status', 'healthy');
});

协作和文档化

工作空间管理

工作空间类型

  • 个人工作空间:个人项目和学习
  • 团队工作空间:团队协作和共享
  • 合作伙伴工作空间:外部合作

权限管理

Admin (管理员):
  - 完全控制权限
  - 成员管理
  - 设置修改

Editor (编辑者):
  - 创建/编辑集合
  - 运行测试
  - 查看所有内容

Viewer (查看者):
  - 只读访问
  - 运行现有测试
  - 无法修改

API文档生成

自动文档生成

  1. 集合右侧菜单 > "View Documentation"
  2. 添加描述和示例
  3. 发布到Postman公共文档或私有文档

文档优化技巧

## 请求描述模板

### 获取用户信息
此端点用于获取指定用户的详细信息。

**参数说明:**
- `user_id` (required): 用户唯一标识符

**响应示例:**
```json
{
  "status": "success",
  "data": {
    "id": 123,
    "name": "John Doe",
    "email": "[email protected]"
  }
}

错误代码:

  • 400: 请求参数错误
  • 404: 用户不存在
  • 500: 服务器内部错误

## 高级功能和技巧

### 动态请求链

```javascript
// 链式请求处理
pm.test("Login and get user data", function () {
    // 第一步:登录
    pm.sendRequest({
        url: pm.environment.get("baseUrl") + "/login",
        method: "POST",
        header: {
            "Content-Type": "application/json"
        },
        body: {
            mode: "raw",
            raw: JSON.stringify({
                username: "testuser",
                password: "testpass"
            })
        }
    }, function(err, response) {
        if (!err) {
            const token = response.json().token;
            pm.environment.set("authToken", token);

            // 第二步:获取用户数据
            pm.sendRequest({
                url: pm.environment.get("baseUrl") + "/user/profile",
                method: "GET",
                header: {
                    "Authorization": "Bearer " + token
                }
            }, function(err, userResponse) {
                pm.test("User profile retrieved", function() {
                    pm.expect(userResponse.code).to.equal(200);
                });
            });
        }
    });
});

响应处理和转换

// XML响应处理
const xml2js = require('xml2js');
const parser = new xml2js.Parser();

pm.test("Parse XML response", function() {
    parser.parseString(pm.response.text(), function(err, result) {
        pm.expect(result.root.status[0]).to.equal('success');
    });
});

// CSV响应处理
pm.test("Parse CSV response", function() {
    const csvData = pm.response.text();
    const lines = csvData.split('\n');
    const headers = lines[0].split(',');

    pm.expect(headers).to.include('id');
    pm.expect(headers).to.include('name');
    pm.expect(lines.length).to.be.above(1);
});

// 二进制文件下载验证
pm.test("File download validation", function() {
    pm.expect(pm.response.headers.get('Content-Type')).to.include('application/pdf');
    pm.expect(pm.response.stream.length).to.be.above(1000);
});

自定义函数库

// 在Tests或Pre-request Scripts中定义全局函数
pm.globals.set("commonFunctions", `
    function generateRandomEmail() {
        const timestamp = Date.now();
        return \`user\${timestamp}@example.com\`;
    }

    function validateEmailFormat(email) {
        const emailRegex = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;
        return emailRegex.test(email);
    }

    function formatDate(date) {
        return date.toISOString().split('T')[0];
    }
`);

// 使用全局函数
eval(pm.globals.get("commonFunctions"));
const email = generateRandomEmail();
pm.environment.set("testEmail", email);

故障排除和最佳实践

常见问题解决

问题1:CORS错误

// 解决方案:使用Postman代理或禁用浏览器安全检查
// 在Pre-request Script中添加:
pm.request.headers.add({
    key: "Access-Control-Allow-Origin",
    value: "*"
});

问题2:SSL证书问题

// 在Settings中关闭SSL certificate verification
// 或在代码中忽略证书错误:
pm.sendRequest({
    url: "https://self-signed.badssl.com/",
    method: "GET",
    header: {
        "ignore-certificate-errors": true
    }
});

问题3:请求超时

// 设置合理的超时时间
pm.test("Request completes within timeout", function() {
    pm.expect(pm.response.responseTime).to.be.below(30000); // 30秒
});

性能优化建议

减少不必要的变量操作

// 低效方式
pm.environment.set("temp1", value1);
pm.environment.set("temp2", value2);
pm.environment.set("temp3", value3);

// 高效方式
const tempData = {
    temp1: value1,
    temp2: value2,
    temp3: value3
};
pm.environment.set("tempData", JSON.stringify(tempData));

批量断言

// 使用describe分组测试
pm.test("User validation suite", function() {
    const user = pm.response.json();

    pm.expect(user).to.have.property('id').that.is.a('number');
    pm.expect(user).to.have.property('name').that.is.a('string');
    pm.expect(user).to.have.property('email').that.matches(/^[^\s@]+@[^\s@]+\.[^\s@]+$/);
    pm.expect(user.age).to.be.at.least(0).and.at.most(150);
});

安全最佳实践

敏感信息保护

// 使用环境变量存储敏感信息
const apiKey = pm.environment.get("API_KEY");
const secret = pm.environment.get("CLIENT_SECRET");

// 避免在脚本中硬编码密钥
// ❌ 错误做法
const apiKey = "sk-1234567890abcdef";

// ✅ 正确做法
const apiKey = pm.environment.get("API_KEY") || pm.globals.get("API_KEY");

日志安全

// 避免记录敏感信息
console.log("API Response:", {
    status: pm.response.status,
    responseTime: pm.response.responseTime,
    // 不要记录包含敏感数据的完整响应
});

// 使用过滤器
function logSafeData(data) {
    const safeCopy = { ...data };
    delete safeCopy.password;
    delete safeCopy.apiKey;
    delete safeCopy.token;
    console.log(safeCopy);
}

通过系统学习Postman的各项功能,你将能够:

  • 高效地进行API开发和测试
  • 建立完整的自动化测试流程
  • 提升团队协作效率
  • 确保API的质量和性能
  • 建立可维护的测试文档

记住,Postman不仅仅是一个测试工具,更是现代API开发工作流的中心枢纽。掌握它将极大提升你的开发效率和产品质量。

Postman API测试完整教程 | MatrixTools