温馨提示×

怎样在ubuntu跑postman脚本

小樊
44
2026-10-15 16:28:01
栏目: 智能运维

在 Ubuntu 上“跑 Postman 脚本”通常有两种理解,下面分别说明,你可以按需要选择。


一、在 Ubuntu 上运行 Postman 里的接口测试(推荐:Newman)

Postman 官方在 Linux 上没有好用的命令行 GUI 脚本执行方式,标准做法是:

用 Postman 导出 Collection → 用 Newman 跑

1. 安装 Node.js(Newman 依赖)

sudo apt update
sudo apt install -y nodejs npm

验证:

node -v
npm -v

2. 安装 Newman

sudo npm install -g newman

验证:

newman --version

3. 从 Postman 导出 Collection

  • 打开 Postman
  • 选择 Collection → 右上角 ... → Export
  • 保存为 collection.json

(如果有环境变量,也导出 environment.json)

4. 在 Ubuntu 运行脚本

newman run collection.json

带环境变量:

newman run collection.json -e environment.json

生成 HTML 报告(可选):

npm install -g newman-reporter-htmlextra
newman run collection.json -r htmlextra

二、在 Ubuntu 上直接使用 Postman(GUI)

如果你只是想在 Ubuntu 上用 Postman 界面跑请求:

方法 1:官方安装(推荐)

sudo apt install -y wget gpg
wget -qO - https://dl.postman.co/schema/keyring.gpg | gpg --dearmor > postman.gpg
sudo install -D -o root -g root -m 644 postman.gpg /etc/apt/keyrings/postman.gpg
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/postman.gpg] https://dl.postman.co/repo/apt/ stable main" | sudo tee /etc/apt/sources.list.d/postman.list
sudo apt update
sudo apt install postman

启动:

postman

方法 2:Snap

sudo snap install postman

三、如果你说的“脚本”是 Pre-request / Tests 脚本

这些不能单独跑,必须:

  • 在 Collection 里
  • 用 Newman 或 Postman 执行请求时触发

例如:

// Tests 脚本
pm.test("status is 200", () => {
  pm.response.to.have.status(200);
});

四、CI / 自动化场景(常见)

newman run collection.json -e env.json --reporters cli,junit

可接 Jenkins / GitLab CI / GitHub Actions。


如果你能具体说一下:

  • 是 接口测试 还是 自动化脚本
  • 是否要 CI 跑
  • 是否已有 Postman Collection

我可以直接给你一条完整可复制的命令。

0 踩