简体   繁体   中英

Mark Gradle build unstable in Jenkins when JUnit tests fail

I have a Gradle build in Jenkins with various JUnit tests that are executed as part of the build. Now when some of the tests fail the complete build is marked as failed - because Gradle says the build failed.

How can I convince Gradle to succeed the build and then Jenkins to mark the build as unstable? With ant this was no problem at all.

Use the ignoreFailures property in the test task.

apply plugin: 'java'
test {
     ignoreFailures = true
}

You can use external properties to solve this problem.

if (!ext.has('ignoreTestFailures')) {
  ext.ignoreTestFailures = false
}

test {
  ignoreFailures = project.ext.ignoreTestFailures
}

In this setup by default failures will fail the build. But if you call Gradle like so: gradle -PignoreTestFailures=true test then the test failures will not fail the build. So you can configure Jenkins to ignore test failures, but to fail the build when a developer runs the tests manually.

You can include this in your main build.gradle to be applied to all projects and all test tasks.

allprojects{
    tasks.withType(Test) {
        ignoreFailures=true;
    }
}

Since just ignoring the failed test could not be used in my case i found out the following. If you are using a scripted jenkinsfile. It is possible to wrap your test stage in a try-catch statement.

try {
 stage('test') {
  sh './gradlew test'
 } 
} catch (e) {
  echo "Test FAILED"
}

This will catch the build exception thrown by gradle but it marks the build as unstable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM