Newman 执行 + Jenkins 集成完整命令脚本

Newman + Jenkins 完整集成全套脚本&操作文档

一、前置环境准备(Linux Jenkins服务器)

1. 安装 Node.js(newman依赖node/npm)

bash 复制代码
# CentOS/RHEL
curl -fsSL https://rpm.nodesource.com/setup_18.x | bash -
yum install -y nodejs

# Ubuntu/Debian
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
apt install -y nodejs

# 验证安装
node -v
npm -v

2. 全局安装 newman & html报告插件

bash 复制代码
# 核心运行工具
npm install -g newman
# 生成美观HTML测试报告
npm install -g newman-reporter-html
# 可选:生成JUnit xml报告,Jenkins可直接解析展示用例结果
npm install -g newman-reporter-junitfull

3. Postman导出文件准备

  1. 导出集合:Collections → 对应集合右上角 ... → Export → 保存为 api_collection.json
  2. 导出环境变量:Environments → 对应环境 → Export → 保存为 env_dev.json
  3. 把两个文件上传到Jenkins服务器项目目录(如 /opt/postman/

二、Newman 本地执行完整命令(可直接Linux运行)

基础最简执行命令

bash 复制代码
newman run /opt/postman/api_collection.json \
-e /opt/postman/env_dev.json

完整版:生成HTML + JUnit双报告(推荐流水线使用)

bash 复制代码
newman run /opt/postman/api_collection.json \
-e /opt/postman/env_dev.json \
# 迭代次数(数据驱动CSV参数化时用)
--iteration-count 1 \
# 超时时间 30000ms=30秒
--timeout-request 30000 \
# 禁止控制台冗余日志
--silent \
# 生成html报告,指定输出路径
-r html,junitfull \
--reporter-html-export /opt/postman/report/api_test.html \
--reporter-junitfull-export /opt/postman/report/api_result.xml

带CSV数据驱动批量执行(DDT)

bash 复制代码
newman run /opt/postman/api_collection.json \
-e /opt/postman/env_dev.json \
# 传入csv参数文件
-d /opt/postman/test_data.csv \
--timeout-request 30000 \
-r html,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安装必要插件

  1. 系统管理 → 插件管理 → 可选插件
  2. 安装:
    • NodeJS Plugin(提供node/npm环境)
    • JUnit Plugin(解析newman生成的xml测试结果)
    • HTML Publisher(展示html可视化测试报告)
  3. 安装后重启Jenkins

步骤2:配置NodeJS工具

  1. 系统管理 → 全局工具配置 → NodeJS
  2. 新增NodeJS,命名 Node18,版本选择18.x,保存

步骤3:新建自由风格流水线项目

① 通用配置
  • 项目名称:接口自动化回归测试
  • 勾选:丢弃旧的构建(按需设置保存构建天数)
② 构建环境

勾选 提供Node & npm ,选择刚才配置的 Node18

③ 构建步骤 → 执行shell(复制下面完整脚本)
bash 复制代码
#!/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-request 30000 \
-r html,junitfull \
--reporter-html-export ${REPORT_DIR}/api_test_report.html \
--reporter-junitfull-export ${REPORT_DIR}/api_junit_result.xml

# 4. 判断执行结果:newman用例失败会返回非0退出码,终止构建
if [ $? -ne 0 ]; then
    echo "===== Postman接口自动化存在失败用例 ====="
    exit 1
fi
④ 构建后操作(2个核心配置)
  1. 发布JUnit测试结果

    • 测试报告XML:postman_report/api_junit_result.xml
    • 勾选:允许空结果、保留测试运行器输出
      作用:Jenkins主页展示用例通过率、失败用例数量
  2. 发布HTML报告

    • HTML目录:postman_report
    • 报告文件名:api_test_report.html
    • 报告名称:接口自动化测试报告
    • 勾选:允许JavaScript(否则报告图表不展示)
      作用:构建完成后可直接点开可视化详细报告

步骤4:定时自动回归(可选)

项目配置 → 构建触发器 → 定时构建

示例(每天凌晨2点自动执行接口回归):

复制代码
0 2 * * *

场景 2:代码提交触发(Git WebHook)

Jenkins 开启「GitHub hook trigger for GITScm polling」

Git 仓库配置 WebHook,推送地址:http://Jenkins地址/github-webhook/

代码合并自动跑接口自动化,阻断上线

场景 3:分离环境(测试 / 预发两套环境文件)

Shell 脚本动态切换环境:

bash 复制代码
# 传入参数区分环境,DEV=测试,PRE=预发
```bash
ENV_TYPE="DEV"
if [ ${ENV_TYPE} == "DEV" ];then
  ENV_FILE="/opt/postman/dev_env.json"
else
  ENV_FILE="/opt/postman/pre_env.json"
fi

newman run 集合.json -e ${ENV_FILE} -r html

四、Pipeline流水线脚本(Jenkinsfile,推荐企业使用)

新建流水线项目,流水线脚本如下,可直接复制:

groovy 复制代码
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 "接口自动化测试存在失败用例,请查看报告排查!"
        }
    }
}

五、常见问题&踩坑解决方案

  1. newman: command not found

    全局npm安装路径未加入系统环境变量,执行 npm config get prefix 把输出目录加入PATH;或在shell脚本内用绝对路径调用newman。

  2. HTML报告打开空白、图表不显示

    发布HTML报告时必须勾选「允许JavaScript」,Jenkins默认拦截JS。

  3. 接口执行超时大量失败

    调整 --timeout-request 参数,加大超时时间(如60000=60秒);检查测试服务器网络连通性。

  4. 环境变量不生效

    确认 -e 指定的环境文件导出完整,token、域名等变量存在;不要混淆环境变量/全局变量。

  5. CSV数据驱动读取不到参数

    CSV第一行必须是参数名,和Postman请求中 {``{参数名}} 完全一致;文件编码为UTF-8,无多余空行。

  6. Jenkins构建因用例失败终止

    newman执行后返回码非0,shell脚本中 exit 1 标记构建失败;如需失败不阻断流水线,删除 if [ $? -ne 0 ]; then exit 1; fi 判断。


六、拓展:钉钉/企业微信失败告警脚本

在shell脚本末尾追加,用例失败自动推送报告链接到群:

bash 复制代码
# 定义钉钉机器人webhook地址
DING_WEBHOOK="https://oapi.dingtalk.com/robot/send?access_token=xxx"
if [ $? -ne 0 ]; then
curl ${DING_WEBHOOK} \
-H "Content-Type: application/json" \
-d '{
    "msgtype": "text",
    "text": {
        "content": "【接口自动化告警】存在失败接口,请查看Jenkins报告:'${env.BUILD_URL}'"
    }
}'
exit 1
fi