Newman 执行 + Jenkins 集成完整命令脚本 Newman Jenkins 完整集成全套脚本操作文档一、前置环境准备Linux Jenkins服务器1. 安装 Node.jsnewman依赖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\# 超时时间 30000ms30秒--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数据驱动批量执行DDTnewman 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-exporthtml报告输出路径–reporter-junitfull-exportxml结果文件Jenkins识别–silent简化控制台输出减少日志刷屏三、Jenkins 完整集成配置步骤步骤1Jenkins安装必要插件系统管理 → 插件管理 → 可选插件安装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.jsonENV_FILE/opt/postman/env_dev.jsonREPORT_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测试结果测试报告XMLpostman_report/api_junit_result.xml勾选允许空结果、保留测试运行器输出作用Jenkins主页展示用例通过率、失败用例数量发布HTML报告HTML目录postman_report报告文件名api_test_report.html报告名称接口自动化测试报告勾选允许JavaScript否则报告图表不展示作用构建完成后可直接点开可视化详细报告步骤4定时自动回归可选项目配置 → 构建触发器 → 定时构建示例每天凌晨2点自动执行接口回归0 2 * * *场景 2代码提交触发Git WebHookJenkins 开启「GitHub hook trigger for GITScm polling」Git 仓库配置 WebHook推送地址http://Jenkins地址/github-webhook/代码合并自动跑接口自动化阻断上线场景 3分离环境测试 / 预发两套环境文件Shell 脚本动态切换环境# 传入参数区分环境DEV测试PRE预发bashENV_TYPEDEVif[${ENV_TYPE}DEV];thenENV_FILE/opt/postman/dev_env.jsonelseENV_FILE/opt/postman/pre_env.jsonfinewman run 集合.json-e${ENV_FILE}-rhtml四、Pipeline流水线脚本Jenkinsfile推荐企业使用新建流水线项目流水线脚本如下可直接复制pipeline{agent any tools{nodejsNode18// 和全局配置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参数加大超时时间如6000060秒检查测试服务器网络连通性。环境变量不生效确认-e指定的环境文件导出完整token、域名等变量存在不要混淆环境变量/全局变量。CSV数据驱动读取不到参数CSV第一行必须是参数名和Postman请求中{{参数名}}完全一致文件编码为UTF-8无多余空行。Jenkins构建因用例失败终止newman执行后返回码非0shell脚本中exit 1标记构建失败如需失败不阻断流水线删除if [ $? -ne 0 ]; then exit 1; fi判断。六、拓展钉钉/企业微信失败告警脚本在shell脚本末尾追加用例失败自动推送报告链接到群# 定义钉钉机器人webhook地址DING_WEBHOOKhttps://oapi.dingtalk.com/robot/send?access_tokenxxxif[$?-ne0];thencurl${DING_WEBHOOK}\-HContent-Type: application/json\-d{ msgtype: text, text: { content: 【接口自动化告警】存在失败接口请查看Jenkins报告${env.BUILD_URL} } }exit1fi