Gradle Goodness: Stop Build After One Failing Test
Normally when we run tests in our Gradle build, all our tests are executed and at the end we can see which tests are failing. But what if we want to let the build fail at the first failing test? Especially for a large test suite this can save a lot of time, because we don’t have to run all (failing) tests, we immediately get informed that at least one test is failing.
We can do this by passing the command-line option --fail-fast
when we run the test
task in Gradle. With this option Gradle will stop the build and report a failure at the first failing test. Instead of passing the command-line option --fail-fast
we can set the property failFast
of the test
task to true
. Using the property failFast
allows to still fail the build on the first failing test even if we for example run a build
task that depends on the test
task. The command-line option --fail-fast
only works if we run the test
task directly, not if it is part of the task graph for our build when we run another task.
In the following sample we have two failing tests in our project. When we run our test
task we see this in the output of our Gradle build:
$ gradle test
mrhaki.gradle.GreetingSpec > greeting should return 'Hello' followed by input FAILED
org.spockframework.runtime.SpockComparisonFailure at GreetingSpec.groovy:12
mrhaki.gradle.HelloSpec > message should return 'Hello' FAILED
org.spockframework.runtime.SpockComparisonFailure at HelloSpec.groovy:12
2 tests completed, 2 failed
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///Users/mrhaki/Projects/mrhaki.com/blog/posts/samples/gradle/testfast/build/reports/tests/test/index.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
3 actionable tasks: 1 executed, 2 up-to-date
Now let’s run the test
task with the command-line option --fail-fast
:
$ gradle test --fail-fast -q
mrhaki.gradle.GreetingSpec > greeting should return 'Hello' followed by input FAILED
org.spockframework.runtime.SpockComparisonFailure at GreetingSpec.groovy:12
2 tests completed, 2 failed
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///Users/mrhaki/Projects/mrhaki.com/blog/posts/samples/gradle/testfast/build/reports/tests/test/index.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
3 actionable tasks: 1 executed, 2 up-to-date
Although the totals indicate 2 tests completed, 2 failed
when we use the fail fast option we can see in the output the test in the HelloSpec
specification is not run this time.
Instead of using the command-line option --fail-fast
we can set the property failFast
in our test
task, like in the following example Gradle build:
...
test {
failFast = true
}
...
Written with Gradle 5.6.2.