如何在 Jenkins Pipeline 项目中访问 Junit 测试计数 [英] How to access Junit test counts in Jenkins Pipeline project
问题描述
我刚开始接触詹金斯
我的自由式项目曾经像这样在 Slack 中报告 JUnit 测试结果
My freestyle project used to report JUnit tests results in Slack like this
MyJenkinsFreestyle - #79 Unstable after 4 min 59 sec (Open)
Test Status:
Passed: 2482, Failed: 13, Skipped: 62
现在我已将相同的内容移至管道项目,除了 Slack 通知没有测试状态外,一切都很好
Now I have moved the same to pipeline project, and all is good except that Slack notifications do not have Test Status
done MyPipelineProject #68 UNSTABLE
我知道我必须构建要发送到 Slack 的消息,我现在已经完成了上述工作.
I understand I have to construct the message to send to Slack, and I have done that above for now.
唯一的问题是我如何读取测试状态 - 通过的计数、失败的计数等.这在 Jenkins slack-plugin
The only issue is how do I read the test status - the passed count, failed count etc. This is called "test summary" in Jenkins slack-plugin commit, and here is the screenshot
那么我如何访问 Jenkins Pipeline 项目中的 Junit 测试计数/详细信息?- 以便在通知中报告这些内容.
So how do I access Junit tests count/details in Jenkins Pipeline project ? - so that these are reported in notifications.
更新:在 Freestyle 项目中,Slack 通知本身就有测试摘要",并没有选择(或不)测试摘要的选项.
UPDATE: In the Freestyle project, the Slack notification itself has the "test summary", and there is no option to opt (or not) for the test summary.
在 Pipeline 项目中,我用于发布 JUnit 测试结果"的junit"命令是在发送 Slack 通知之前.
In Pipeline project, my "junit" command to "Publish JUnit test results" is before sending Slack notification.
所以在代码中这些行看起来像这样(这是最后阶段的最后几行):
So in code those lines look like this (this are last lines of the last stage):
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>)";
推荐答案
来自 此演示文稿 Cloudbees 我发现它应该可以通过构建"对象实现.它有像
From this presentation of Cloudbees I found that it should be possible via "build" object. It has code like
def testResult = build.testResultAction
def total = testResult.totalCount
但是 currentBuild 不提供对 testResultAction 的访问.
But currentBuild does not provide access to testResultAction.
所以继续搜索并找到了这篇文章"对失败的测试做出反应在管道脚本中".罗伯特·桑德尔 (Robert Sandell) 给出了专业提示"
So kept searching and found this post "react on failed tests in pipeline script". There Robert Sandell has given "pro tip"
专业提示,需要一些自定义白名单":
Pro tip, requires some "custom whitelisting":
AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
if (testResultAction != null) {
echo "Tests: ${testResultAction.failCount} / ${testResultAction.failureDiffString} failures of ${testResultAction.totalCount}.
"
}
这很有效 - 只是我必须取消选中Groovy 沙箱"复选框.现在我在构建日志中有这些
This worked like a charm - just that I had to deselect "Groovy sandbox" checkbox. Now I have these in the build log
Tests: 11 / ±0 failures of 2624
现在我将使用它来准备字符串以在松弛时通知测试结果.
Now I will use this to prepare string to notify in slack with test results.
更新:
最后,我用来获取输出的函数如下(注意测试失败后的失败差异"非常有用)
Finally, the function I used to get output like the following (Note the "failure diff" after failed tests is very useful)
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:
Passed: ${passed}, Failed: ${failed} ${testResultAction.failureDiffString}, Skipped: ${skipped}"
if (failed == 0) {
currentBuild.result = 'SUCCESS'
}
}
return testStatus
}
<小时>
更新 2018-04-19
请注意,以上要求手动将所用方法列入白名单".这是一次性将所有方法列入白名单的方法
Note the above require manual "whitelisting" of methods used. Here is how you can whitelist all the methods in one go
手动更新白名单...
退出詹金斯
使用以下内容创建/更新 %USERPROFILE%.jenkinsscriptApproval.xml
Create/Update %USERPROFILE%.jenkinsscriptApproval.xml with the following content
<?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>
- 重启詹金斯
- 然后验证脚本内批准"是否已批准上述条目
- 注意:这很重要.因此,如果 scriptApproval 文件已经存在,那么您通常需要确保标记的内容.
这篇关于如何在 Jenkins Pipeline 项目中访问 Junit 测试计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!