Question

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.

Was it helpful?

Solution

Use the ignoreFailures property in the test task.

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top