一、Ubuntu下Postman安装方法
Snap安装(推荐,简单快捷)
打开终端,依次执行以下命令安装Snapd(若未安装)及Postman:
sudo apt update && sudo apt install snapd # 安装Snapd
sudo snap install postman --classic # 安装Postman(--classic模式允许无沙盒运行)
安装完成后,终端输入postman即可启动,或在应用菜单中搜索“Postman”。
手动下载安装(自定义路径)
.tar.gz格式);/opt目录:cd ~/Downloads
sudo tar -xzf Postman-linux-x64-*.tar.gz -C /opt/
sudo ln -s /opt/Postman/Postman /usr/bin/postman
/usr/share/applications/postman.desktop文件,写入以下内容并赋予执行权限:[Desktop Entry]
Encoding=UTF-8
Name=Postman
Exec=/opt/Postman/Postman
Icon=/opt/Postman/app/resources/app/assets/icon.png
Terminal=false
Type=Application
Categories=Development;
执行sudo chmod +x /usr/share/applications/postman.desktop。二、Postman基础使用流程(API测试)
创建请求
启动Postman后,点击左上角“+”按钮新建请求。选择HTTP方法(GET/POST/PUT/DELETE等),输入API URL(如http://localhost:8080/api/users)。
配置请求参数
Content-Type: application/json、Authorization: Bearer <token>);?key1=value1&key2=value2(如分页参数?page=1&limit=10);/users/{{id}}),可通过“Params”面板输入变量值;{"name": "John", "email": "john@example.com"})。发送请求与查看响应
点击右上角“Send”按钮,下方“Response”面板将显示:
Content-Type: application/json);三、高级功能:提升测试效率
环境变量与全局变量
base_url)。点击“Environment quick look”→“Add”,定义变量(如base_url: http://localhost:8080),在请求URL中使用{{base_url}}/api/users;token: abc123)。测试脚本(断言验证)
在“Tests”标签页编写JavaScript断言,自动验证响应。常见示例:
pm.test("Status code is 200", () => pm.response.to.have.status(200));
pm.test("Response has user ID", () => {
const jsonData = pm.response.json();
pm.expect(jsonData).to.have.property("id");
});
pm.test("User name matches request", () => {
const requestData = JSON.parse(pm.request.body.raw);
const responseData = pm.response.json();
pm.expect(responseData.name).to.eql(requestData.name);
});
发送请求后,点击“Test Results”面板查看断言结果。
集合(Collections)与批量运行
自动化测试(Newman)
Newman是Postman的命令行工具,可将集合集成到CI/CD管道。安装Newman:
npm install -g newman
导出Postman集合为JSON文件(点击集合→“Export”),运行命令:
newman run my_collection.json -e my_env.json -r html
其中-e指定环境变量文件,-r html生成HTML报告(保存至当前目录)。
四、注意事项
sudo apt update && sudo apt upgrade);pm.response.json()用于解析JSON响应,若响应为XML需使用pm.response.text();sudo apt install nodejs npm)。