Jenkins运行Newman执行Postman集合生成报告找不到测试文件报错如何解决
问题根因
你遇到的报错是因为Pipeline配置和执行逻辑存在以下几处问题:
- 未提前创建
newman输出目录:Newman不会自动生成不存在的父目录,指定的报告导出路径如果父目录不存在,报告会生成失败,导致后续阶段找不到目标文件 - publishHTML插件参数配置错误:
reportDir已经指定了基础目录为newman,reportFiles只需要填写该目录下的报告文件名即可,不需要重复写全路径 - 单引号导致变量无法解析:Jenkins Pipeline中单引号包裹的字符串不会自动解析
$WORKSPACE这类内置变量,会被当做普通文本处理,导致路径匹配失败 - junit路径配置冗余:junit插件默认从当前构建工作目录(即
$WORKSPACE)查找文件,不需要额外添加$WORKSPACE前缀
修复方案
按以下步骤调整配置即可解决:
- 执行Newman命令前先创建
newman目录,避免报告导出失败 - 修正publishHTML的
reportFiles参数为实际的HTML报告文件名 - 路径配置直接使用相对路径,避免变量解析问题
- 调整junit的路径为相对路径
修正后的完整Pipeline
currentBuild.displayName="Prod-API-Automation-#"+currentBuild.number pipeline{ agent any tools {nodejs "NodeJS"} stages{ stage("Building"){ steps{ sh 'npm install -g newman' sh 'npm install -g newman-reporter-htmlextra' } } stage("Execute Collection"){ steps{ script{ try{ // 提前创建输出目录 sh 'mkdir -p newman' sh "newman run Test.postman_collection.json -e Test.postman_environment.json -r cli,junit,htmlextra --reporter-junit-export newman/prod_report.xml --reporter-htmlextra-export newman/prod_Report.html" currentBuild.result="SUCCESS" }catch(Exception ex){ currentBuild.result="FAILURE" } } } } stage("Generating Report"){ steps{ publishHTML([allowMissing: false, alwaysLinkToLastBuild: true, keepAll: false, reportDir: 'newman', reportFiles: 'prod_Report.html', reportName: 'HTML Report', reportTitles: '']) junit 'newman/prod_report.xml' } } } }
验证说明
调整后执行构建,会先创建newman目录再导出报告,生成的报告文件会存放在工作目录的newman文件夹下,publishHTML和junit插件可以正常识别到对应文件,不会再抛出找不到报告的错误。
内容的提问来源于stack exchange,提问作者Manish Katepallewar
相关产品推荐
相关产品推荐

