如何在Jenkins Pipeline项目中访问Junit测试计数 [英] How to access Junit test counts in Jenkins Pipeline project

查看:324
本文介绍了如何在Jenkins Pipeline项目中访问Junit测试计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始接触詹金斯

我的自由式项目用于像这样在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 commit 中称为测试摘要",这是屏幕截图

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>)";

推荐答案

来自

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}.\n\n" 
}

这就像一种魅力-只是我不得不取消选择"Groovy sandbox"复选框. 现在我在构建日志中有这些

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:\n  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

手动更新白名单...

Manually update the whitelist...

退出詹金斯

使用以下内容创建/更新%USERPROFILE%.jenkins \ scriptApproval.xml

Create/Update %USERPROFILE%.jenkins\scriptApproval.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文件已经存在,则通常需要确保tag的内容.
  • 这篇关于如何在Jenkins Pipeline项目中访问Junit测试计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆