Newman + Jenkins 完整集成全套脚本&操作文档
一、前置环境准备(Linux Jenkins服务器)
1. 安装 Node.js(newman依赖node/npm)
# CentOS/RHELcurl-fsSLhttps://rpm.nodesource.com/setup_18.x|bash- yuminstall-ynodejs# Ubuntu/Debiancurl-fsSLhttps://deb.nodesource.com/setup_18.x|bash-aptinstall-ynodejs# 验证安装node-vnpm-v2. 全局安装 newman & html报告插件
# 核心运行工具npminstall-gnewman# 生成美观HTML测试报告npminstall-gnewman-reporter-html# 可选:生成JUnit xml报告,Jenkins可直接解析展示用例结果npminstall-gnewman-reporter-junitfull3. Postman导出文件准备
- 导出集合:Collections → 对应集合右上角
...→ Export → 保存为api_collection.json - 导出环境变量:Environments → 对应环境 → Export → 保存为
env_dev.json - 把两个文件上传到Jenkins服务器项目目录(如
/opt/postman/)
二、Newman 本地执行完整命令(可直接Linux运行)
基础最简执行命令
newman run /opt/postman/api_collection.json\-e/opt/postman/env_dev.json完整版:生成HTML + JUnit双报告(推荐流水线使用)
newman run /opt/postman/api_collection.json\-e/opt/postman/env_dev.json\# 迭代次数(数据驱动CSV参数化时用)--iteration-count1\# 超时时间 30000ms=30秒--timeout-request30000\# 禁止控制台冗余日志--silent\# 生成html报告,指定输出路径-rhtml,junitfull\--reporter-html-export /opt/postman/report/api_test.html\--reporter-junitfull-export /opt/postman/report/api_result.xml带CSV数据驱动批量执行(DDT)
newman run /opt/postman/api_collection.json\-e/opt/postman/env_dev.json\# 传入csv参数文件-d/opt/postman/test_data.csv\--timeout-request30000\-rhtml,junitfull\--reporter-html-export /opt/postman/report/api_test.html\--reporter-junitfull-export /opt/postman/report/api_result.xml参数说明
| 参数 | 作用 |
|---|---|
| run 集合文件 | 指定要执行的Postman集合 |
| -e 环境文件 | 加载环境变量(token、域名、测试地址) |
| -d csv文件 | 数据驱动,每行一组测试参数 |
| –iteration-count N | 循环执行N次集合 |
| –timeout-request | 单接口超时毫秒 |
| -r html,junitfull | 指定生成两种格式报告 |
| –reporter-html-export | html报告输出路径 |
| –reporter-junitfull-export | xml结果文件,Jenkins识别 |
| –silent | 简化控制台输出,减少日志刷屏 |
三、Jenkins 完整集成配置步骤
步骤1:Jenkins安装必要插件
- 系统管理 → 插件管理 → 可选插件
- 安装:
- NodeJS Plugin(提供node/npm环境)
- JUnit Plugin(解析newman生成的xml测试结果)
- HTML Publisher(展示html可视化测试报告)
- 安装后重启Jenkins
步骤2:配置NodeJS工具
- 系统管理 → 全局工具配置 → NodeJS
- 新增NodeJS,命名
Node18,版本选择18.x,保存
步骤3:新建自由风格流水线项目
① 通用配置
- 项目名称:接口自动化回归测试
- 勾选:丢弃旧的构建(按需设置保存构建天数)
② 构建环境
勾选提供Node & npm,选择刚才配置的Node18
③ 构建步骤 → 执行shell(复制下面完整脚本)
#!/bin/bash# 1. 定义文件路径COLLECTION_FILE="/opt/postman/api_collection.json"ENV_FILE="/opt/postman/env_dev.json"REPORT_DIR="./postman_report"# 2. 创建报告目录,清空历史报告rm-rf${REPORT_DIR}mkdir-p${REPORT_DIR}# 3. 执行newman自动化测试newman run${COLLECTION_FILE}\-e${ENV_FILE}\--timeout-request30000\-rhtml,junitfull\--reporter-html-export${REPORT_DIR}/api_test_report.html\--reporter-junitfull-export${REPORT_DIR}/api_junit_result.xml# 4. 判断执行结果:newman用例失败会返回非0退出码,终止构建if[$?-ne0];thenecho"===== Postman接口自动化存在失败用例 ====="exit1fi④ 构建后操作(2个核心配置)
发布JUnit测试结果
- 测试报告XML:
postman_report/api_junit_result.xml - 勾选:允许空结果、保留测试运行器输出
作用:Jenkins主页展示用例通过率、失败用例数量
- 测试报告XML:
发布HTML报告
- HTML目录:
postman_report - 报告文件名:
api_test_report.html - 报告名称:接口自动化测试报告
- 勾选:允许JavaScript(否则报告图表不展示)
作用:构建完成后可直接点开可视化详细报告
- HTML目录:
步骤4:定时自动回归(可选)
项目配置 → 构建触发器 → 定时构建
示例(每天凌晨2点自动执行接口回归):
0 2 * * *场景 2:代码提交触发(Git WebHook)
Jenkins 开启「GitHub hook trigger for GITScm polling」
Git 仓库配置 WebHook,推送地址:http://Jenkins地址/github-webhook/
代码合并自动跑接口自动化,阻断上线
场景 3:分离环境(测试 / 预发两套环境文件)
Shell 脚本动态切换环境:
# 传入参数区分环境,DEV=测试,PRE=预发```bashENV_TYPE="DEV"if[${ENV_TYPE}=="DEV"];thenENV_FILE="/opt/postman/dev_env.json"elseENV_FILE="/opt/postman/pre_env.json"finewman run 集合.json-e${ENV_FILE}-rhtml四、Pipeline流水线脚本(Jenkinsfile,推荐企业使用)
新建流水线项目,流水线脚本如下,可直接复制:
pipeline{agent any tools{nodejs'Node18'// 和全局配置Node名称保持一致}stages{stage('初始化环境'){steps{sh''' node -v npm -v newman -v '''}}stage('执行Postman自动化'){steps{sh''' rm -rf postman_report mkdir -p postman_report newman run /opt/postman/api_collection.json \ -e /opt/postman/env_dev.json \ --timeout-request 30000 \ -r html,junitfull \ --reporter-html-export postman_report/api_test_report.html \ --reporter-junitfull-export postman_report/api_junit_result.xml '''}}}post{always{// 无论成功失败都解析测试结果junit allowEmptyResults:true,testResults:'postman_report/api_junit_result.xml'// 发布HTML报告publishHTML target:[allowMissing:false,alwaysLinkToLastBuild:true,keepAll:true,reportDir:'postman_report',reportFiles:'api_test_report.html',reportName:'Postman接口自动化报告']}failure{// 用例失败输出告警,可对接企业微信/钉钉通知echo"接口自动化测试存在失败用例,请查看报告排查!"}}}五、常见问题&踩坑解决方案
newman: command not found
全局npm安装路径未加入系统环境变量,执行npm config get prefix把输出目录加入PATH;或在shell脚本内用绝对路径调用newman。HTML报告打开空白、图表不显示
发布HTML报告时必须勾选「允许JavaScript」,Jenkins默认拦截JS。接口执行超时大量失败
调整--timeout-request参数,加大超时时间(如60000=60秒);检查测试服务器网络连通性。环境变量不生效
确认-e指定的环境文件导出完整,token、域名等变量存在;不要混淆环境变量/全局变量。CSV数据驱动读取不到参数
CSV第一行必须是参数名,和Postman请求中{{参数名}}完全一致;文件编码为UTF-8,无多余空行。Jenkins构建因用例失败终止
newman执行后返回码非0,shell脚本中exit 1标记构建失败;如需失败不阻断流水线,删除if [ $? -ne 0 ]; then exit 1; fi判断。
六、拓展:钉钉/企业微信失败告警脚本
在shell脚本末尾追加,用例失败自动推送报告链接到群:
# 定义钉钉机器人webhook地址DING_WEBHOOK="https://oapi.dingtalk.com/robot/send?access_token=xxx"if[$?-ne0];thencurl${DING_WEBHOOK}\-H"Content-Type: application/json"\-d'{ "msgtype": "text", "text": { "content": "【接口自动化告警】存在失败接口,请查看Jenkins报告:'${env.BUILD_URL}'" } }'exit1fi