24
votes

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.

4

4 Answers

25
votes

Use the ignoreFailures property in the test task.

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

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.

2
votes

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;
    }
}
0
votes

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.