Test Impact Analysis

You can extend the capabilities of the Jtest Plugin for Gradle with test impact analysis. It allows you to you to identify and re-run only the tests that are affected by your changes, eliminating the time and effort required to execute a large number of unaffected tests. To perform test impact analysis of your project, you need to:

  1. Configure the test impact analysis plugin.
  2. Execute the affectedTests task.

You don't need to modify the Gradle build script.

Prerequisites

(info) Test impact analysis may require additional memory. We recommend increasing the memory allocated to the Gradle build.

Integrating with the Test Impact Analysis Plugin

The init.gradle script shipped with Jtest allows you to integrate the test impact analysis plugin with Gradle – without having to modify your Gradle build script. To integrate Gradle with the plugin, pass the location of the the init.gradle script with the -I option to your command line:

gradle affectedTests test -I PATH/TO/JTEST/integration/gradle/init.gradle

Configuring the Plugin

You can customize test impact analysis of your project in the build file or in the command line by configuring the properties of the test impact analysis plugin. At minimum, you must provide the paths to the following files that are generated by Jtest during execution:

See Jtest Tasks Reference for Gradle for the complete list of available options.

In the build script

Specify the properties when declaring a configuration in the build script:

affectedTests {
    referenceCoverageFile = 'path/to/coverage.xml'
    referenceReportFile = 'path/to/report.xml'
    runFailedTests = false
    runModifiedTests = true
    jtestHome = 'path/to/jtest'
    settings = 'path/to/jtestcli.properties'
}

In the Command Line

If you configure the plugin in the command line, pass the plugin properties with the -D switch. The properties must include the "jtest" prefix (see Jtest Tasks Reference for Gradle). Your command line may resemble the following:

gradle affectedTests test -I PATH/TO/JTEST/integration/gradle/init.gradle -Djtest.referenceCoverageFile="path/to/coverage.xml" -Djtest.referenceReportFile="path/to/report.xml" -Djtest.runFailedTests=false -Djtest.runModifiedTests=true -Djtest.home="path/to/jtest" -Djtest.settings="jtestcli.properties" 

Configuring and Executing the affectedTestTask

You can execute the affectedTests task from the command line without any further configuration. Ensure it is executed before the test task. Your command line may resemble the following:

gradle clean affectedTests test -I PATH/TO/JTEST/integration/gradle/init.gradle

Alternatively, you can:

  1. Configure execution the task in the build.gradle script:

    test.dependsOn affectedTests
  2. Execute the the Gradle test task – the affectedTests task will be executed automatically:

    gradle test -I PATH/TO/JTEST/integration/gradle/init.gradle

If you want to run the affectedTests task for selected subprojects of a multi-module project, modify your command line to only execute the Gradle test task for the subprojects you want to test. Your command line may resemble the following:

gradle clean affectedTests subproject1:test subproject2:test -Djtest.referenceCoverageFile=tia/coverage.xml -Djtest.referenceReportFile=tia/report.xml -I path_jtest\integration\gradle\init.gradle

Gradle's limitation in handling nested tests affects how test impact analysis re-runs tests that are nested in another test. As a result, if the runModifiedTests option is set to true, nested tests are always re-executed, even if they are not affected by code changes.

This Gradle issue has been reproduced with Gradle 6.0.1 and earlier.

Re-executing Test Suites

Test impact analysis re-runs the entire test suite when at least one test included in that test suit is affected by code changes. Note that this may result in re-running some tests that are not affected if they are included in the same test suite as affected tests.