如果单元测试失败,Jenkins脚本化管道不会获取测试结果 [英] Jenkins scripted pipeline does not fetch the test result if unit test fails

查看:118
本文介绍了如果单元测试失败,Jenkins脚本化管道不会获取测试结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有基于gradle的带有junit测试的Java项目,我正在为其构建CI作业.我使用松弛Slack Notification插件成功地将松弛与Jenkins集成.

I have gradle based java projects with junit tests which I'm building CI jobs for. I was successfully able to integrate slack with Jenkins using slack Slack Notification plugin.

Jenkins版本:2.173
松弛通知版本:2.20

Jenkins Version : 2.173
Slack Notification version : 2.20

jenkins CI是一个脚本化管道,其具有以下代码:

The jenkins CI is a scripted pipeline, which has the below code:


stage('Test') {
        def slackHelper = new com.xyz.jenkins.libraries.SlackNotifier(env)
        try {
            sh "./gradlew test"
            junit 'build/test-results/test/*.xml'
        } finally {
            AbstractTestResultAction testResultAction =  currentBuild.rawBuild.getAction(AbstractTestResultAction.class)

            slackHelper.getTestStatuses(currentBuild)
            slackSend(channel: '#ci-cd', attachments: slackHelper.buildUnitTestSlackNotificationMessage())
        }
    }

SlackNotifier是一个包含以下代码的库:

SlackNotifier is a library which has the below code:

/**
 * Calculates test result as a string
 * @param currentBuild : jenkins object, should be passed from jenkins pipeline script
 * @return the final test result as a string
 */
@NonCPS
def getTestStatuses(currentBuild) {
    final AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
    if (testResultAction != null) {
        this.total = testResultAction.totalCount
        this.failed = testResultAction.failCount
        this.skipped = testResultAction.skipCount
        this.passed = total - failed - skipped
    }
}

buildUnitTestSlackNotificationMessage在同一类中执行此操作:

buildUnitTestSlackNotificationMessage does this in the same class:

def buildUnitTestSlackNotificationMessage() {
        final JSONObject unitTestResult = new JSONObject()
        unitTestResult.put("fallback", this.jenkinsEnvironment.JOB_NAME + "with build#" + this.jenkinsEnvironment.BUILD_NUMBER + "finish with unit test result : Passed: " + this.passed + " | Failed: " + this.failed + " | Skipped: " + this.skipped )
        unitTestResult.put("color", this.getUnitTestReportColor())
        unitTestResult.put("pretext", "Message from CI job: " + this.jenkinsEnvironment.JOB_NAME + "#" + this.jenkinsEnvironment.BUILD_NUMBER)
        unitTestResult.put("title", "BuildLog")
        unitTestResult.put("title_link", "<<<JenkinsHost>>>" + this.jenkinsEnvironment.JOB_NAME + "/" + this.jenkinsEnvironment.BUILD_NUMBER  + "/console")
        unitTestResult.put("text", "Passed: " + this.passed +  " | Failed: " + this.failed + " | Skipped: " + this.skipped)
        unitTestResult.put("image_url", this.getLogoURL())
        this.attachments.add(unitTestResult)
        return this.attachments.toString()
    }

所有测试通过时,一切都很好.但是当测试失败时,我会收到以下通知:

All is well when all the tests pass. But when tests fail I get the below notification:

Message from CI job: <<<JobName>>>#47
BuildLog
Passed: null | Failed: null | Skipped: null

结果表明,当任何单元测试在此处失败时,testResultAction为空.

It turns out testResultAction is null when any unit tests fail here.

我无法深入了解这一点.请帮忙.

And I'm unable to come to the bottom of this. Please help.

推荐答案

我在reddit中得到了答案,功劳归于:/u/Bodumin

I got the answer in reddit, credit goes to : /u/Bodumin

这是根本原因,我在这里引用他:

And here is the root cause, I'm quoting him here :

Move the junit step into the finally. What's likely happening is that test returns a non 0 (error) status so it fails it of the try.

因此,脚本化管道如下所示:

So, the scripted pipeline looks like below:

stage('Test') {
        def slackHelper = new com.xyz.jenkins.libraries.SlackNotifier(env)
        try {
            sh "./gradlew test"
        } finally {
            junit 'build/test-results/test/*.xml'
            AbstractTestResultAction testResultAction =  currentBuild.rawBuild.getAction(AbstractTestResultAction.class)

            slackHelper.getTestStatuses(currentBuild)
            slackSend(channel: '#ci-cd', attachments: slackHelper.buildUnitTestSlackNotificationMessage())
        }
    }

这篇关于如果单元测试失败,Jenkins脚本化管道不会获取测试结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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