Skip to main content

How to abort a Pipeline build if JUnit tests fail?

Comments

3 comments

  • Lionel Orellana

    I would actually like the build to remain UNSTABLE when tests fail. From what I've seen the build will be marked as FAILURE when you throw err even if you don't set the result explicitly. The question then becomes how to abort the pipeline leaving the result as UNSTABLE.   

     

     

    0
  • Evgeny Zislis

    The way it is described in this article, test results archiver will only work when the command failed. Most probably this is not what you want, you want the archiver to always do its work - both for failed runs and for successful test runs.

    A better way would be something like this -

    try {
      // test stuff
    }
    catch(err) {
    // handle the exception; or ignore it
    } finally {
    step([$class: 'JUnitResultArchiver', testResults: '**/TEST-*.xml'])
    }

    Another option is to allow the exception to be raised, but still archive the test results before raising it:

    try {
      // test stuff; notice there is no '} catch(err) {'
    } finally {
    step([$class: 'JUnitResultArchiver', testResults: '**/TEST-*.xml']) }
    0
  • Philip Cheong

    @Lionel With the version 2.4 of Pipeline Nodes and Processes Plugin that supports additional ways of handling shell scripts, this code works pretty well for me...

    node ('master') {
    stage "build"
    // build without tests
    checkout scm
    sh './gradlew clean build -x test -x integTest'

    stage "unit test"
    // returnStatus: true here will ensure the build stays yellow
    // when test cases are failing
    sh (script: './gradlew test', returnStatus: true)
    step([$class: 'JUnitResultArchiver',
    testResults: '**/build/test-results/test/TEST-*.xml'])

    if (currentBuild.result == null) {
     // if unit tests have failed currentBuild will be 'UNSTABLE'
    // and we should not bother to run integration tests
    stage "integration test"
    sh (script: './gradlew integTest', returnStatus: true)
    step([$class: 'JUnitResultArchiver',
    testResults: '**/build/test-results/integTest/TEST-*.xml'])
    }
    }

     

    1

Please sign in to leave a comment.

About CloudBees Support

Our Support Engineers are available to help with any questions or problems you may have with any of our products.