如何访问Jenkins Pipeline项目中的Junit测试计数

vik*_*sjn 18 groovy junit jenkins slack-api jenkins-pipeline

我刚开始和詹金斯在一起

我的自由式项目用于报告Slack中的JUnit测试结果

MyJenkinsFreestyle - #79 Unstable after 4 min 59 sec (Open)
Test Status:
    Passed: 2482, Failed: 13, Skipped: 62
Run Code Online (Sandbox Code Playgroud)

现在我已将其移至管道项目,除了Slack通知没有测试状态外,一切都很好

done MyPipelineProject #68 UNSTABLE
Run Code Online (Sandbox Code Playgroud)

我知道我必须构建要发送给Slack的消息,我现在已经完成了上述操作.

唯一的问题是如何读取测试状态 - 传递计数,失败计数等.这在Jenkins slack-plugin 提交中称为"测试摘要" ,这是截图 testsummaryimage

那么如何在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>)";
Run Code Online (Sandbox Code Playgroud)

Sky*_*ton 63

对于 2020 年来到这里的任何人来说,现在似乎有一种更简单的方法。对“junit testResults”的调用返回一个TestResultSummary对象,该对象可以分配给一个变量并在以后使用。

作为通过 slack 发送摘要的示例:

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}"
)
Run Code Online (Sandbox Code Playgroud)

  • 请注意,正如 @StaxMan 提到的,由于这会存档测试,请确保您没有任何存档它们的步骤。我正在这样做,我观察到 Jenkins UI 报告了重复的计数,这非常令人困惑 (2认同)

vik*_*sjn 31

从Cloudbees的这个演示中我发现应该可以通过"构建"对象.它的代码就像

def testResult = build.testResultAction
def total = testResult.totalCount
Run Code Online (Sandbox Code Playgroud)

但是currentBuild不提供对testResultAction的访问.

所以继续搜索,发现这篇文章"对管道脚本中的失败测试作出反应".Robert Sandell已经给出了"专业提示"

专业提示,需要一些"自定义白名单":

AbstractTestResultAction testResultAction =  currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
if (testResultAction != null) {
    echo "Tests: ${testResultAction.failCount} / ${testResultAction.failureDiffString} failures of ${testResultAction.totalCount}.\n\n" 
}
Run Code Online (Sandbox Code Playgroud)

这就像一个魅力 - 只是我必须取消选择"Groovy沙箱"复选框.现在我在构建日志中有这些

Tests: 11  / ±0 failures of 2624
Run Code Online (Sandbox Code Playgroud)

现在我将使用它来准备字符串以通知测试结果.


更新:

最后,我用来获得如下输出的函数(注意失败测试后的"失败差异"非常有用)

Test Status:
  Passed: 2628, Failed: 6  / ±0, Skipped: 0
Run Code Online (Sandbox Code Playgroud)

以下是:

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
}
Run Code Online (Sandbox Code Playgroud)

更新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>
Run Code Online (Sandbox Code Playgroud)
  • 重启Jenkins
  • 然后验证"脚本批准"是否已批准上述条目
  • 注意:这是重要的.因此,如果scriptApproval文件已经存在,那么您通常需要确保标记的内容.

  • 自定义白名单在这里非常重要。它默默地失败了,直到我访问 jenkins.server.example.com:8080/scriptApproval/ 并单击批准*几个*调用。请注意,我必须在作业运行尝试之间执行几次此操作,因为一次批准会解锁下一次批准。 (2认同)

Chr*_*ung 10

要扩展@ 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
}
Run Code Online (Sandbox Code Playgroud)

然后我使用此方法实例化我的testSummary变量:

def testSummary = getTestSummary()
Run Code Online (Sandbox Code Playgroud)

这将返回类似于:

"Test results:
     Passed: 123, Failed: 0, Skipped: 0"
Run Code Online (Sandbox Code Playgroud)