Postman实战案例:从零开始设计API测试流程

Postman实战案例:从零开始设计API测试流程

API测试在现代软件开发中至关重要。Postman作为一款强大的API测试工具,不仅提供了直观的用户界面,还支持自动化测试、环境配置和脚本编写。本文将从零开始,详细介绍如何使用Postman设计API测试流程,涵盖基础知识、环境配置、请求创建、测试编写、集合管理和自动化测试等内容,并附带具体代码示例。

1. Postman基础知识

Postman是一款广泛使用的API开发工具,主要功能包括:

  • 创建和发送HTTP请求
  • 查看响应数据
  • 组织请求集合
  • 编写测试脚本
  • 环境变量管理
  • 集成自动化测试
安装Postman

Postman提供了跨平台的桌面应用程序和浏览器扩展。你可以从Postman官网下载并安装适合你的版本。

2. 创建Postman请求

一个Postman请求包括以下几个部分:

  • 请求方法(GET、POST、PUT、DELETE等)
  • 请求URL
  • 请求头
  • 请求体
  • 脚本(Pre-request Script和Tests)
示例:创建一个GET请求

假设我们要测试一个公开的API,如GitHub的用户信息API:

  1. 打开Postman,点击"New"按钮,选择"Request"。
  2. 输入请求名称,如"Get GitHub User Info"。
  3. 在请求方法中选择"GET"。
  4. 在URL栏中输入API的URL,例如https://api.github.com/users/{username}
  5. 点击"Send"按钮,查看响应数据。
json 复制代码
GET https://api.github.com/users/octocat

响应示例:

json 复制代码
{
  "login": "octocat",
  "id": 1,
  "node_id": "MDQ6VXNlcjE=",
  "avatar_url": "https://avatars.githubusercontent.com/u/1?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/octocat",
  "html_url": "https://github.com/octocat",
  "followers_url": "https://api.github.com/users/octocat/followers",
  "following_url": "https://api.github.com/users/octocat/following{/other_user}",
  "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
  "organizations_url": "https://api.github.com/users/octocat/orgs",
  "repos_url": "https://api.github.com/users/octocat/repos",
  "events_url": "https://api.github.com/users/octocat/events{/privacy}",
  "received_events_url": "https://api.github.com/users/octocat/received_events",
  "type": "User",
  "site_admin": false,
  "name": "The Octocat",
  "company": "@github",
  "blog": "https://github.blog",
  "location": "San Francisco",
  "email": null,
  "hireable": null,
  "bio": null,
  "twitter_username": null,
  "public_repos": 8,
  "public_gists": 8,
  "followers": 4152,
  "following": 9,
  "created_at": "2011-01-25T18:44:36Z",
  "updated_at": "2021-05-13T19:10:57Z"
}

3. 环境配置

在实际测试中,我们通常需要对不同环境进行测试,比如开发环境、测试环境和生产环境。Postman通过环境变量来实现这一点。

创建环境
  1. 点击Postman右上角的齿轮图标,选择"Manage Environments"。
  2. 点击"Add",创建一个新的环境,例如"Development"。
  3. 添加变量,如base_url,值为https://api.github.com
使用环境变量

在请求URL中使用环境变量:

json 复制代码
{{base_url}}/users/{{username}}

在发送请求前,选择相应的环境,Postman会自动替换URL中的变量。

4. 编写测试脚本

Postman允许在请求后编写测试脚本,以验证响应数据。测试脚本使用JavaScript编写,可以访问响应数据和环境变量。

示例:验证GitHub用户信息
javascript 复制代码
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test("Response has login field", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('login');
});

pm.test("User is Octocat", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.login).to.eql('octocat');
});

5. 组织请求集合

在实际项目中,我们通常需要测试多个API接口。Postman提供了请求集合(Collection)功能,方便管理和组织多个请求。

创建请求集合
  1. 点击Postman左侧栏的"Collections"标签,点击"New Collection"按钮。
  2. 输入集合名称,如"GitHub API Tests"。
  3. 将之前创建的请求拖动到集合中。

6. 自动化测试

Postman支持将请求集合导出为JSON文件,并通过Newman命令行工具运行,实现自动化测试。

安装Newman
bash 复制代码
npm install -g newman
导出请求集合
  1. 选择请求集合,点击右上角的"...",选择"Export"。
  2. 选择导出格式为JSON文件。
运行自动化测试
bash 复制代码
newman run path/to/collection.json -e path/to/environment.json

7. 进阶功能

Postman还提供了许多高级功能,如监控、Mock Server和API文档生成。

使用监控

监控功能允许你定期运行请求集合,并发送运行结果到指定邮箱。

  1. 选择请求集合,点击右上角的"...",选择"Monitor Collection"。
  2. 配置监控频率和通知方式。
使用Mock Server

Mock Server允许你在API尚未实现时,模拟API响应,方便前端开发和测试。

  1. 点击Postman左侧栏的"Mock Servers"标签,点击"Create a Mock Server"。
  2. 配置Mock Server的URL和响应数据。
生成API文档

Postman可以自动生成API文档,方便团队共享。

  1. 选择请求集合,点击右上角的"...",选择"Publish Docs"。
  2. 配置文档的公开性和访问权限。

总结

本文详细介绍了如何使用Postman从零开始设计API测试流程,涵盖了创建请求、环境配置、编写测试、组织请求集合和自动化测试等内容。通过这些步骤,你可以高效地进行API测试,确保API的可靠性和稳定性。

Postman的强大功能不仅限于此,随着你对工具的深入了解,还可以探索更多高级功能,如监控、Mock Server和API文档生成,以提升团队的开发和测试效率。希望本文能对你有所帮助,让你在API测试之旅中更加得心应手。

附录:完整测试脚本示例

以下是一个完整的测试脚本示例,用于验证GitHub用户信息API:

javascript 复制代码
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});

pm.test("Content-Type is application/json", function () {
    pm.response.to.have.header("Content-Type", "application/json; charset=utf-8");
});

pm.test("Response has login field", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData).to.have.property('login');
});

pm.test("User is Octocat", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.login).to.eql('octocat');
});

pm.test("Avatar URL is valid", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.avatar_url).to.match(/^https:\/\/avatars\.githubusercontent\.com\/u\/\d+\?v=\d+$/);
});

pm.test("User is not a site admin", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.site_admin).to.be.false;
});

参考文献

相关推荐
Lossya5 小时前
【自动化测试】常见的自动化遍历工具以及如何选择合适的自动化遍历工具
自动化测试·功能测试·测试工具·自动化·测试
Lossya9 小时前
【自动化测试】UI自动化的分类、如何选择合适的自动化测试工具以及其中appium的设计理念、引擎和引擎如何工作
自动化测试·测试工具·ui·appium·自动化
chenjingming6661 天前
windows使用tcpdump.exe工具进行抓包教程
网络·测试工具·tcpdump
小码哥说测试1 天前
软件测试技术之 GPU 单元测试是什么!
自动化测试·功能测试·测试工具·jmeter·单元测试·集成测试·postman
Lill_bin1 天前
Lua编程语言简介与应用
开发语言·数据库·缓存·设计模式·性能优化·lua
两水先木示2 天前
【Lua坑】Lua协程coroutine无法正常完整执行问题
开发语言·lua·协程·对象池
XXX-172 天前
3.postman脚本语言、接口关联(json引用(变量)、脚本用正则表达式)、断言封装、自动化构造接口请求(Postman工具)
软件测试·接口测试·postman
全能全知者2 天前
不废话简单易懂的Selenium 页面操作与切换
python·selenium·测试工具·网络爬虫
测试19982 天前
使用Selenium进行网页自动化
自动化测试·软件测试·python·selenium·测试工具·自动化·测试用例
做一道光2 天前
1、QAC静态测试常用操作
软件测试·测试工具·静态测试