25

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.

tobiasbayer
  • 10,269
  • 4
  • 46
  • 64
Lasrik
  • 589
  • 1
  • 8
  • 22

4 Answers4

25

Use the ignoreFailures property in the test task.

apply plugin: 'java'
test {
     ignoreFailures = true
}
forresthopkinsa
  • 1,339
  • 1
  • 24
  • 29
lucas
  • 6,951
  • 4
  • 33
  • 34
  • Is possible to configure this for jenkins only from commandline? Because with this option FAILED build is logged as SUCCESS – MariuszS Feb 15 '14 at 09:54
  • 3
    good build succeed - but jenkins shows build as "success" and not as "unstable" :-/. Anyone has good solution for this? Some plugin for Jenkins? or something – bugs_ Oct 15 '15 at 08:41
  • 1
    well - solution is use this. And something from this answers http://stackoverflow.com/questions/8148122/how-to-mark-a-build-unstable-in-jenkins-when-running-shell-scripts I used "Text-finder" plugin – bugs_ Oct 21 '15 at 13:57
4

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.

jgibson
  • 1,013
  • 11
  • 9
2

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;
    }
}
Pedro Fraca
  • 185
  • 9
0

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.