Skip to content
Latchkey

gradle test: Usage, Options & Common CI Errors

Run unit tests - and know why Gradle sometimes reports them UP-TO-DATE.

test is the Gradle task that runs the project’s unit tests using the configured test framework (JUnit, TestNG, Spock). It writes results to build/reports/tests and build/test-results and fails the build on any test failure.

What it does

The test task compiles test sources and executes them. It is incremental: if nothing it depends on changed, Gradle marks it UP-TO-DATE and skips it. --tests filters by class/method, and maxParallelForks controls parallel test JVMs.

Common usage

Terminal
./gradlew test
./gradlew test --tests "com.example.UserServiceTest"
./gradlew test --tests "*UserServiceTest.createsUser"
./gradlew test --rerun-tasks       # force re-run, ignoring UP-TO-DATE

Common error in CI (and the fix)

Symptom: tests show "UP-TO-DATE" and do not actually run in CI, so failures are never caught. Cause: Gradle’s incremental build / build cache considered inputs unchanged. Fix: this is usually correct behavior, but to force execution (e.g. for flaky or environment-dependent tests) use --rerun-tasks, or mark the test task to always run; do not disable the cache globally just to force one task.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →