【问题标题】:How to distinguish failed tests from syntax error in jenkins pipeline如何在詹金斯管道中区分失败的测试和语法错误
【发布时间】:2019-01-03 16:56:56
【问题描述】:

我有这个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.

【问题讨论】:

  • 只是一个简短的评论,但看起来您的失败阈值是 '' = '0'。您可以尝试使用“2”作为“不稳定”的设置吗?
  • @Luc 失败的阈值在这里不起作用。我会更新我的代码,因为我真的不需要它。

标签: jenkins groovy


【解决方案1】:

来自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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 2022-11-11
    • 2017-05-04
    相关资源
    最近更新 更多