1. 前置准备:安装Postman并创建测试集合
在Linux系统(如Ubuntu)上,首先需要安装Postman客户端。访问Postman官网下载Linux版本安装包(如.tar.gz格式),解压后进入解压目录,运行./Postman/Postman启动应用。创建测试集合:在Postman左侧“Collections”面板点击“New Collection”,输入集合名称(如“API批量测试”),点击“Create”。将需要批量测试的API请求添加到集合中——可通过“Save”按钮将现有请求存入集合,或直接在集合内点击“Add Request”创建新请求。
2. 编写测试脚本(可选但推荐)
为确保API响应符合预期,需在每个请求的“Tests”标签页编写JavaScript测试脚本。常见断言包括:
pm.test("Status code is 200", function () { pm.response.to.have.status(200); });pm.test("Body contains expected data", function () { const responseJson = pm.response.json(); pm.expect(responseJson.fieldName).to.eql("expectedValue"); });pm.test("Response time is less than 200ms", function () { pm.expect(pm.response.responseTime).to.be.below(200); });3. 配置参数化数据(可选,用于动态测试)
若需用不同数据批量测试同一接口,需创建CSV或JSON格式的数据文件。CSV文件示例(首行为变量名):
username,password
test1,123456
test2,222333
JSON文件示例:
[{"username": "test1", "password": "123456"}, {"username": "test2", "password": "222333"}]
在请求参数中使用{{variable_name}}格式引用变量(如{{username}}、{{password}}),Runner会自动将数据文件中的每一行数据替换到对应变量中。
4. 启动Postman Runner并设置运行参数
启动Runner有两种方式:
5. 执行批量测试并查看结果
配置完成后,点击“Run [集合名称]”按钮开始批量执行。Runner会自动运行集合中的所有请求,并在界面中实时展示结果: