如何在jenkins管道中区分失败的测试和语法错误

时间:2018-05-09 09:19:17

标签: jenkins groovy

我有Jenkinsfile

node{
  stage ('Checkout')
  {
    checkout scm
  }
  stage ('Build')
  {
    try {
        sh '''
           mvn clean -B org.jacoco:jacoco-maven-plugin:prepare-agent install
        '''
    } catch (err) {
        // do nothing
       currentBuild.result = 'FAILED'
    } finally {
        //step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
       step([$class: 'XUnitBuilder', testTimeMargin: '3000', thresholdMode: 1,
    thresholds: [
        [$class: 'FailedThreshold',   unstableThreshold: '2'],
        [$class: 'SkippedThreshold',  unstableThreshold: '']],
    tools: [
        [$class: 'JUnitType', deleteOutputFiles: false, failIfNotNew: false, pattern: '**/target/surefire-reports/TEST-*.xml', skipNoTestFiles: true, stopProcessingIfError: false]]
    ])
    }
  }
}

我想在以下情况下将构建标记为UNSTABLE:某些测试使用xUnit插件的阈值失败,如果代码中出现任何错误(如语法错误),我想将其标记为FAILED

这里的问题是catch在两种情况下都会执行:代码错误和测试失败。如何配置Jenkinsfile以区别对待?

运行有3次测试失败(阈值为2)的构建后,我预计构建为UNSTABLE但转向FAILED

[xUnit] [INFO] - Starting to record.
[xUnit] [INFO] - Processing JUnit
[xUnit] [INFO] - [JUnit] - 1 test report file(s) were found with the pattern '**/target/surefire-reports/TEST-*.xml' relative to '/var/lib/jenkins/workspace/jenkins-failure_master-EHKMAGGDJWHA7FNGHQYY2VJNJHBX2RXI3XJY4NU5EB2PYT4ERTZQ' for the testing framework 'JUnit'.
[xUnit] [INFO] - Check 'Failed Tests' threshold.
[xUnit] [INFO] - The total number of tests for this category exceeds the specified 'unstable' threshold value.
[xUnit] [INFO] - Setting the build status to FAILURE
[xUnit] [INFO] - Stopping recording.

新案例:我评论了行currentBuild.result = 'FAILED',并在测试类中添加了语法错误。即使使用COMPILATION ERROR,构建也会成功,并且它会跳过测试:

[xUnit] [INFO] - Starting to record.
[xUnit] [INFO] - Processing JUnit
[xUnit] [INFO] - [JUnit] - No test report file(s) were found with the pattern '**/target/surefire-reports/TEST-*.xml' relative to '/var/lib/jenkins/workspace/jenkins-failure_master-EHKMAGGDJWHA7FNGHQYY2VJNJHBX2RXI3XJY4NU5EB2PYT4ERTZQ' for the testing framework 'JUnit'.  Did you enter a pattern relative to the correct directory?  Did you generate the result report(s) for 'JUnit'?
[xUnit] [WARNING] - No test reports found for the metric 'JUnit' with the resolved pattern '**/target/surefire-reports/TEST-*.xml'.
[xUnit] [INFO] - Skipping the metric tool processing.
[xUnit] [INFO] - There are errors when processing test results.
[xUnit] [INFO] - Skipping tests recording.

1 个答案:

答案 0 :(得分:0)

来自Reporting test results and storing artifacts(声明性管道):

pipeline {
    agent { docker "java" }
    stages {
        stage("build") {
            steps {
                sh 'mvn clean install -Dmaven.test.failure.ignore=true'
            }
        }
    }
    post {
        always {
            archive "target/**/*"
            junit 'target/surefire-reports/*.xml'
        }
    }
}

使用-Dmaven.test.failure.ignore=true可以防止基于测试失败的构建失败。然后,junit步骤将收集测试结果并将其标记为不稳定。

使用非管道Maven作业时,这也是在后台进行的操作,请参见JENKINS-24655