Postman在Linux中的集成方法主要包括安装部署、自动化测试及CI/CD融合三大类,以下是详细步骤:
Snap是Linux系统的软件包管理工具,安装Postman无需手动配置依赖。操作步骤如下:
sudo apt update && sudo apt upgrade -y # Ubuntu/Debian
sudo yum update -y # CentOS/RHEL
sudo apt install snapd -y # 安装Snap
sudo snap install postman
postman即可启动;或在应用菜单中搜索“Postman”。若系统不支持Snap,可通过下载官方安装包手动部署:
wget https://dl.pstmn.io/download/latest/linux64 -O postman.tar.gz
/opt):sudo tar -xzf postman.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;
保存后,即可在应用菜单中找到Postman图标。Postman的图形界面适合手动测试,而Newman是其官方提供的命令行工具,可实现自动化测试与CI/CD融合。
Newman依赖Node.js环境,需先安装Node.js和npm:
# Ubuntu/Debian
sudo apt install nodejs npm -y
# CentOS/RHEL
sudo yum install nodejs npm -y
全局安装Newman:
sudo npm install -g newman
.json文件(如collection.json);environment.json)。newman run /path/to/collection.json
newman run /path/to/collection.json -e /path/to/environment.json
sudo npm install -g newman-reporter-html
运行命令时添加报告参数:newman run /path/to/collection.json -e /path/to/environment.json -r html --reporter-html-export /path/to/reports.html
将Newman命令写入Shell脚本(如run_postman.sh),实现一键运行:
#!/bin/bash
COLLECTION="/path/to/collection.json"
ENVIRONMENT="/path/to/environment.json"
REPORT="/path/to/reports.html"
newman run "$COLLECTION" -e "$ENVIRONMENT" -r html --reporter-html-export "$REPORT"
echo "测试完成,报告路径:$REPORT"
赋予执行权限并运行:
chmod +x run_postman.sh
./run_postman.sh
将Postman测试集成到CI/CD流程,实现代码提交后自动运行测试。
newman run /var/lib/jenkins/workspace/api-test/collection.json -e /var/lib/jenkins/workspace/api-test/environment.json -r html --reporter-html-export /var/lib/jenkins/workspace/api-test/reports.html
.github/workflows/postman.yml文件,内容如下:name: Postman API Test
on: [push, pull_request] # 触发条件:代码推送或拉取请求
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 16
- name: Install Newman
run: npm install -g newman
- name: Run Postman tests
run: |
newman run ./collections/collection.json -e ./environments/environment.json -r html --reporter-html-export ./reports.html
- name: Upload report
uses: actions/upload-artifact@v3
with:
name: postman-report
path: ./reports.html
此配置会在每次代码推送或拉取请求时,自动运行Postman测试并上传HTML报告。若API需通过代理访问,可在Postman中配置代理:
http://proxy.example.com)和端口(如8080);以上方法覆盖了Postman在Linux中的基础安装、自动化测试及CI/CD融合场景,可根据实际需求选择合适的方式。