我刚刚开始使用Jenkins
我的自由式项目用于报告像这样的Slack中的JUnit测试结果
MyJenkinsFreestyle - #79 Unstable after 4 min 59 sec (Open)
Test Status:
Passed: 2482, Failed: 13, Skipped: 62
现在我已经将它移动到管道项目,除了Slack通知没有测试状态
之外一切都很好done MyPipelineProject #68 UNSTABLE
我知道我必须构建要发送给Slack的消息,我现在已经完成了这个。
唯一的问题是如何读取测试状态 - 传递的计数,失败的计数等。 这在Jenkins slack-plugin commit中称为“测试摘要”,这是截图
那么如何在Jenkins Pipeline项目中访问Junit测试计数/详细信息? - 以便在通知中报告这些内容。
更新: 在Freestyle项目中,Slack通知本身具有“测试摘要”,并且没有选择(或不选择)测试摘要。
在Pipeline项目中,我的“junit”命令“发布JUnit测试结果”是在发送Slack通知之前。
所以在代码中这些行看起来像这样(这是最后一个阶段的最后几行):
bat runtests.bat
junit 'junitreport/xml/TEST*.xml'
slackSend channel: '#testschannel', color: 'normal', message: "done ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)";
答案 0 :(得分:45)
对于2020年来这里的人来说,现在似乎有一种更简单的方法。调用'junit testResults'返回一个TestResultSummary对象,该对象可以分配给变量并在以后使用。
作为通过松弛发送摘要的示例:
def summary = junit testResults: '/somefolder/*-reports/TEST-*.xml'
slackSend (
channel: "#mychannel",
color: '#007D00',
message: "\n *Test Summary* - ${summary.totalCount}, Failures: ${summary.failCount}, Skipped: ${summary.skipCount}, Passed: ${summary.passCount}"
)
答案 1 :(得分:27)
从Cloudbees的this presentation我发现它应该可以通过“build”对象实现。 它的代码类似于
def testResult = build.testResultAction
def total = testResult.totalCount
但是currentBuild不提供对testResultAction的访问。
所以继续搜索并找到这篇文章"react on failed tests in pipeline script"。 Robert Sandell已经给了"pro tip"
专业提示,需要一些“自定义白名单”:
AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class) if (testResultAction != null) { echo "Tests: ${testResultAction.failCount} / ${testResultAction.failureDiffString} failures of ${testResultAction.totalCount}.\n\n" }
这就像一个魅力 - 只是我不得不取消选择“Groovy sandbox”复选框。 现在我在构建日志中有这些
Tests: 11 / ±0 failures of 2624
现在我将使用它来准备字符串以通知测试结果。
更新:
最后,我用来获取输出的函数如下 (注意失败测试后的“失败差异”非常有用)
Test Status:
Passed: 2628, Failed: 6 / ±0, Skipped: 0
以下是:
import hudson.tasks.test.AbstractTestResultAction
@NonCPS
def testStatuses() {
def testStatus = ""
AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
if (testResultAction != null) {
def total = testResultAction.totalCount
def failed = testResultAction.failCount
def skipped = testResultAction.skipCount
def passed = total - failed - skipped
testStatus = "Test Status:\n Passed: ${passed}, Failed: ${failed} ${testResultAction.failureDiffString}, Skipped: ${skipped}"
if (failed == 0) {
currentBuild.result = 'SUCCESS'
}
}
return testStatus
}
更新2018-04-19
注意上面要求使用手动“白名单”的方法。 以下是一次将所有方法列入白名单的方法
手动更新白名单......
退出詹金斯
使用以下内容创建/更新%USERPROFILE%。jenkins \ scriptApproval.xml
<?xml version='1.0' encoding='UTF-8'?>
<scriptApproval plugin="script-security@1.23">
<approvedScriptHashes>
</approvedScriptHashes>
<approvedSignatures>
<string>method hudson.model.Actionable getAction java.lang.Class</string>
<string>method hudson.model.Cause getShortDescription</string>
<string>method hudson.model.Run getCauses</string>
<string>method hudson.tasks.test.AbstractTestResultAction getFailCount</string>
<string>method hudson.tasks.test.AbstractTestResultAction getFailureDiffString</string>
<string>method hudson.tasks.test.AbstractTestResultAction getSkipCount</string>
<string>method hudson.tasks.test.AbstractTestResultAction getTotalCount</string>
<string>method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild</string>
</approvedSignatures>
<aclApprovedSignatures/>
<approvedClasspathEntries/>
<pendingScripts/>
<pendingSignatures/>
<pendingClasspathEntries/>
</scriptApproval>
答案 2 :(得分:7)
要扩展@ vikramsjn的答案,以下是我用来获取 Jenkinsfile 中的测试摘要的内容:
import hudson.tasks.test.AbstractTestResultAction
import hudson.model.Actionable
@NonCPS
def getTestSummary = { ->
def testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
def summary = ""
if (testResultAction != null) {
def total = testResultAction.getTotalCount()
def failed = testResultAction.getFailCount()
def skipped = testResultAction.getSkipCount()
summary = "Test results:\n\t"
summary = summary + ("Passed: " + (total - failed - skipped))
summary = summary + (", Failed: " + failed)
summary = summary + (", Skipped: " + skipped)
} else {
summary = "No tests found"
}
return summary
}
然后我使用此方法实例化我的testSummary
变量:
def testSummary = getTestSummary()
这将返回类似于:
的内容"Test results:
Passed: 123, Failed: 0, Skipped: 0"
答案 3 :(得分:1)
首先,感谢您提供上述答案。他们节省了我很多时间,我在管道中使用了建议的解决方案。但是,我没有使用“白名单”,而且效果很好。 我将共享库用于Jenkins管道,这是该共享库中带有管道并使用方法获取计数的一部分:
import hudson.model.*
import jenkins.model.*
import hudson.tasks.test.AbstractTestResultAction
def call(Closure body) {
...
def emailTestReport = ""
pipeline {
...
stages{
stage('Test'){
...
post {
always {
junit 'tests.xml'
script {
AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
if (testResultAction != null) {
def totalNumberOfTests = testResultAction.totalCount
def failedNumberOfTests = testResultAction.failCount
def failedDiff = testResultAction.failureDiffString
def skippedNumberOfTests = testResultAction.skipCount
def passedNumberOfTests = totalNumberOfTests - failedNumberOfTests - skippedNumberOfTests
emailTestReport = "Tests Report:\n Passed: ${passedNumberOfTests}; Failed: ${failedNumberOfTests} ${failedDiff}; Skipped: ${skippedNumberOfTests} out of ${totalNumberOfTests} "
}
}
mail to: 'example@email.com',
subject: "Tests are finished: ${currentBuild.fullDisplayName}",
body: "Tests are finished ${env.BUILD_URL}\n Test Report: ${emailTestReport} "
}
}
}
}
}
}
p.s。如果我将emailTestRepot创建为脚本“部分”中的局部变量,则会出现下一个异常:
an exception which occurred:
in field locals
in field parent
in field caller
in field e
in field program
in field threads
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@11cd92de
Caused: java.io.NotSerializableException: hudson.tasks.junit.TestResultAction
...
我在尝试修复该java.io.NotSerializableException时费了很多力气。据我了解,我需要使用“白名单”来防止NotSerializableException。但是我真的不想这么做,当我将“ def emailTestReport”移出管道时,它工作得很好。