Java Programming Tutorials

Java programming tutorials with many code examples!

Gradle continue build even with failed tests

When Gradle is building a project then by default it will fail when any of build tasks fails. But sometimes we may want Gradle to continue build even with failed tests.

A common case for this is project report generators, which should continue even when tests fail.

Grade build with failed tests

When we run Gradle build with failing tests, then the build will fail:

gradle build
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE

...

com.farenda.spock.AutoCleanupTest > should close the resource FAILED
    java.lang.RuntimeException

36 tests completed, 6 failed, 6 skipped
:test FAILED

FAILURE: Build failed with an exception.

What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///home/farenda/spock-tutorial/build/reports/tests/index.html

Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 37.057 secs

Gradle continue build with failing tests

Sometimes we want to continue the build even when same tasks fail or to continue building multi-module project, even when tests in some modules fail. To do that we can start Gradle with –continue option:

gradle --continue clean build

The build as a whole will fail, be not as soon as some task fails. This allows us, for example, to gather reports from execution of tests from every module in multi-module project.

Ignoring failures in Gradle buildscript

To ignore failures in selected build tasks, we can use ignoreFailures setting, which is false by default for the Java task:

test.ignoreFailures = true

Now if we run the same build, but with the above setting, we’ll still have failing tests, but the build will be successful:

$ gradle build
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
...

com.farenda.spock.AutoCleanupTest > should close the resource FAILED
    java.lang.RuntimeException

36 tests completed, 6 failed, 6 skipped
There were failing tests. See the report at: file:///home/farenda/spock-tutorial/build/reports/tests/index.html
:check
:build

BUILD SUCCESSFUL

Total time: 32.939 secs

References:

  • Check out other posts about Gradle!
Share with the World!