1. 准备基础环境
在Ubuntu系统上,首先安装运行Postman测试所需的工具:
newman基于Node.js,需先通过sudo apt update && sudo apt install nodejs npm安装。npm install -g newman,安装后可通过newman --version验证是否成功。2. 创建并导出Postman测试集合
在Postman中完成API测试用例的设计(如接口请求、断言规则),将测试集合保存为JSON格式(通过“Collection → Export”功能导出)。导出的集合文件(如api_tests.json)需上传至代码仓库(如GitHub、GitLab),作为CI/CD流程的输入。
3. 配置CI/CD工具(以GitHub Actions为例)
GitHub Actions是Ubuntu环境下常用的CI/CD工具,通过YAML文件定义自动化流程:
.github/workflows目录下新建postman.yml(如run-postman-tests.yml)。main分支或创建拉取请求时自动运行,示例如下:on:
push:
branches:
- main
pull_request:
branches:
- main
actions/checkout@v3拉取仓库代码;npm install -g newman安装newman(若未提前安装);newman run命令执行导出的集合,添加--reporters cli,junit生成文本和JUnit格式报告,示例如下:- name: Run Postman Tests
run: newman run "api_tests.json" --reporters cli,junit --reporter-junit-export report.xml
actions/upload-artifact@v2),便于后续查看测试详情。4. 处理测试结果
report.xml文件,或通过插件(如Jenkins的JUnit插件)直接展示测试结果;currentBuild.result = 'FAILURE'),并停止后续流程(如部署)。5. 触发与自动化
将工作流文件提交至仓库(git add .github/workflows/postman.yml && git commit -m "Add Postman CI workflow" && git push origin main),此后每当代码推送或拉取请求触发条件时,CI/CD工具会自动执行Postman测试,实现测试自动化。
可选扩展
curl -o- "https://dl-cli.pstmn.io/install/linux64.sh" | sh安装Postman CLI,支持直接登录Postman账号并运行云端集合(需配置API密钥);