Ubuntu 上安装与启动 Postman
sudo snap install postman --classicpostmanwget https://dl.pstmn.io/download/latest/linux64 -O postman.tar.gzsudo tar -xzf postman.tar.gz -C /opt/sudo ln -s /opt/Postman/Postman /usr/local/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 apt install libgconf2-4sudo 直接启动 Postman,防止文件权限问题快速开始一个 API 请求
{{baseUrl}}/users{
"name": "Alice",
"email": "alice@example.com"
}
环境变量与测试脚本
baseUrl=https://api.example.com、token=xxxx{{baseUrl}}、{{token}} 引用pm.test("Status code is 200", () => pm.response.to.have.status(200));
pm.test("Response has userId", () => {
const json = pm.response.json();
pm.expect(json).to.have.property('userId');
});
const json = pm.response.json();
pm.environment.set("token", json.token);
pm.request.headers.add({
key: "Authorization",
value: "Bearer " + pm.environment.get("token")
});
自动化与 CI CD 集成
npm install -g newmannewman run collection.json -e environment.jsonpipeline {
agent any
stages {
stage('Run API Tests') {
steps { script { sh 'newman run collection.json -e environment.json' } }
}
}
}
常见问题与实用建议
sudo 启动 Postmansudo snap refresh postman 更新;压缩包方式需下载最新版本替换并重新创建软链