如果单元测试失败,Jenkins脚本化管道不会获取测试结果

时间:2019-04-16 00:42:09

标签: jenkins groovy continuous-integration jenkins-pipeline

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

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

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


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是一个具有以下代码的库:

/**
 * 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在同一类中执行此操作:

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()
    }

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

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

当这里的任何单元测试失败时,testResultAction为空。

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

1 个答案:

答案 0 :(得分:0)

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

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

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.

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

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())
        }
    }