如何在Jenkins Pipeline控制台打印Robot Framework失败测试信息?
解决Jenkins Pipeline中Robot Framework失败测试信息打印报错问题
错误原因
Jenkins Robot插件提供的robot()方法返回的对象并不包含failedTests这个公开属性,直接访问该属性会触发no such property failedTests错误。
解决方案
方案1:直接解析Robot的output.xml文件
利用Groovy内置的XmlSlurper工具解析测试生成的output.xml,直接提取失败测试的标签、名称和失败原因:
pipeline { agent any stages { stage('Robot Test') { steps { script { // 执行Robot Framework测试 sh 'robot --outputdir results tests' // 解析output.xml获取失败测试详情 def outputXml = new XmlSlurper().parse('results/output.xml') // 遍历所有失败的测试用例 outputXml.testsuite.testcase.findAll { it.status.@status == 'FAIL' }.each { failedTest -> println("=== 失败测试用例详情 ===") println("测试名称: ${failedTest.@name}") println("关联标签: ${failedTest.tags.tag.collect { it.text() }.join(', ')}") println("失败原因: ${failedTest.status.@message}") println("---------------------------") } } } } } }
方案2:使用Robot Framework自带的rebot命令生成摘要
通过rebot命令直接提取失败测试的文本摘要并打印到控制台,无需手动解析XML:
pipeline { agent any stages { stage('Robot Test') { steps { script { // 执行Robot Framework测试 sh 'robot --outputdir results tests' // 生成失败测试的文本摘要并打印 def failedSummary = sh( script: 'rebot --outputdir results --report none --log none --output none --failed results/output.xml', returnStdout: true ).trim() println("=== Robot失败测试摘要 ===") println(failedSummary) } } } } }
注意事项
- 确保Jenkins执行节点已正确安装Robot Framework,
rebot命令可正常调用 - 确认
output.xml的路径与测试执行时指定的输出目录一致
内容的提问来源于stack exchange,提问作者Arunkumar G
相关产品推荐
相关产品推荐

