Gradle - running tests in Continuous Build

In a previous post, we saw Gradle's continuous build in action. If you use Test Driven Development at work, you will be running your tests after every code change. Let's leverage Gradle's continuous build so that you don't have to trigger the tests every time you change your code manually.

My application is in a directory named "continuousbuild", and the structure is as follows.

➜  continuousbuild tree
.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── nareshak
    │   │           └── demo
    │   │               └── App.java
    │   └── resources
    └── test
        ├── java
        │   └── com
        │       └── nareshak
        │           └── demo
        │               └── SampleTest.java
        └── resources

15 directories, 8 files

Let's write a simple failing test as follows.

package com.nareshak.demo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SampleTest {

    @Test
    void test() {
        Assertions.assertTrue(false);
    }
}

Now, let's run the test from the console by invoking the test task. Let's also pass -t to instruct Gradle run in continuous build mode.

➜  continuousbuild gradle test -t
Starting a Gradle Daemon (subsequent builds will be faster)

> Task :test FAILED

SampleTest > test() FAILED
    org.opentest4j.AssertionFailedError at SampleTest.java:10

1 test completed, 1 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/naresha/practice/gradle/continuousbuild/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 8s
3 actionable tasks: 3 executed

Waiting for changes to input files of tasks... (ctrl-d to exit)
new directory: /Users/naresha/practice/gradle/continuousbuild/build/classes/java/test

As expected, the test is failing, and Gradle is waiting for the changes to files. Let's make the test pass by changing our assert statement to Assertions.assertTrue(true).

Waiting for changes to input files of tasks... (ctrl-d to exit)
modified: /Users/naresha/practice/gradle/continuousbuild/src/test/java/com/nareshak/demo/SampleTest.java
Change detected, executing build...


BUILD SUCCESSFUL in 1s
3 actionable tasks: 2 executed, 1 up-to-date

Waiting for changes to input files of tasks... (ctrl-d to exit)
<=============> 100% EXECUTING [2m 14s]

From the terminal logs, we can make out that Gradle has detected changes to the file "SampleTest.java", and it reran the tests.

Last time when we ran the run task, it was watching for the changes under "src/main". However, when you run test, both "src/main" and "src/test" will be watched for changes. You can validate this by modifying the source file "App.java".

Waiting for changes to input files of tasks... (ctrl-d to exit)
modified: /Users/naresha/practice/gradle/continuousbuild/src/main/java/com/nareshak/demo/App.java
Change detected, executing build...


BUILD SUCCESSFUL in 1s
3 actionable tasks: 2 executed, 1 up-to-date

Waiting for changes to input files of tasks... (ctrl-d to exit)
<=============> 100% EXECUTING [5m 40s]