--- title: The JVM Test Suite Plugin source: https://docs.gradle.org/current/userguide/jvm_test_suite_plugin.html tags: - IT/Development/Gradle - IT/Development/Java --- The JVM Test Suite plugin (plugin id: `jvm-test-suite`) provides a DSL and API to model multiple groups of automated tests into test suites in JVM-based projects. Tests suites are intended to grouped by their purpose and can have separate dependencies and use different testing frameworks. For instance, this plugin can be used to define a group of Integration Tests, which might run much longer than unit tests and have different environmental requirements. ## [](#sec:jvm_test_suite_usage)[Usage](#sec:jvm_test_suite_usage) This plugin is applied automatically by the `java` plugin but can be additionally applied explicitly if desired. The plugin cannot be used without a JVM language plugin applied as it relies on several conventions of the `java` plugin. Example 1. Applying the JVM Test Suite plugin build.gradle ```groovy plugins { id 'java' id 'jvm-test-suite' } ``` The plugins adds the following objects to the project: - A `testing` extension (type: [TestingExtension](https://docs.gradle.org/current/dsl/org.gradle.testing.base.TestingExtension.html)) to the project used to configure test suites. - A test suite named `test` (type: [JvmTestSuite](https://docs.gradle.org/current/dsl/org.gradle.api.plugins.jvm.JvmTestSuite.html)). - A `test` [SourceSet](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.SourceSet.html). - Several configurations derived from the `test` SourceSet name: `testImplementation`, `testCompileOnly`, `testRuntimeOnly` - A single test suite target backed by a task named `test`. The `test` task, SourceSet and derived configurations are identical in name and function to those used in prior Gradle releases. ## [](#sec:jvm_test_suite_tasks)[Tasks](#sec:jvm_test_suite_tasks) The JVM Test Suite plugin adds the following task to the project: `test` — [Test](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html) *Depends on*: `testClasses` from the `java` plugin, and all tasks which produce the test runtime classpath Runs the tests using the framework configured for the default test suite. Additional instances of [Test](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html) tasks will be automatically created for each test suite added via the `testing` extension. ## [](#sec:jvm_test_suite_configuration)[Configuration](#sec:jvm_test_suite_configuration) ### [](#sec:jvm_test_suite_terminology)[Terminology and Modeling](#sec:jvm_test_suite_terminology) The JVM Test Suite Plugin introduces some modeling concepts backed by new APIs. Here are their definitions. #### [](#test_suite)[Test Suite](#test_suite) A test suite is a collection of JVM-based tests. #### [](#test_suite_target)[Test Suite Target](#test_suite_target) For the initial release of this plugin, each test suite has a single target. This results in a 1:1:1 relationship between test suite, test suite target and a matching [Test](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html) task. The name of the `Test` task is derived from the suite name. Future iterations of the plugin will allow defining multiple targets based other attributes, such as a particular JDK/JRE runtime. Each test suite has some configuration that is common across for all tests contained in the suite: - Testing framework - Sources - Dependencies In the future, other properties may be specified in the test suite which may influence the toolchains selected to compile and run tests. The `Test` task associated with the target inherits its name from the suite. Other properties of the `Test` task are configurable. #### [](#test_suite_type)[Test Suite Type](#test_suite_type) Each test suite must be assigned a type. Types can be used to group related test suites across multiple Gradle projects within a build. The type of a test suite can be configured using the suites’s [test type property](https://docs.gradle.org/current/dsl/org.gradle.api.plugins.jvm.JvmTestSuite.html#getTestType). The type **must be unique** across all test suites in the same Gradle project. By convention, the type is set to the name of the test suite, converted to dash-case - with the exception of the built-in test suite, which uses the value ['unit-test'](https://docs.gradle.org/current/javadoc/org/gradle/api/attributes/TestSuiteType.html#UNIT_TEST--). Common values are available as constants in [TestSuiteType](https://docs.gradle.org/current/javadoc/org/gradle/api/attributes/TestSuiteType.html). ## Configuration Examples Here are several examples to illustrate the configurability of test suites. ## [](#declare_an_additional_test_suite)[Declare an additional test suite](#declare_an_additional_test_suite) build.gradle ```groovy testing { suites { (1) test { (2) useJUnitJupiter() (3) } integrationTest(JvmTestSuite) { (4) dependencies { implementation project (5) } targets { (6) all { testTask.configure { shouldRunAfter(test) } } } } } } tasks.named('check') { (7) dependsOn(testing.suites.integrationTest) } ``` | | | | --- | --- | | **1** | Configure all test suites for this project. | | **2** | Configure the built-in `test` suite. This suite is automatically created for backwards compatibility. You **must** specify a testing framework to use in order to run these tests (e.g. JUnit 4, JUnit Jupiter). This suite is the only suite that will automatically have access to the production source’s `implementation` dependencies, all other suites must explicitly declare these. | | **3** | Declare this test suite uses JUnit Jupiter. The framework’s dependencies are automatically included. It is always necessary to explicitly configure the built-in `test` suite even if JUnit4 is desired. | | **4** | Define a new suite called `integrationTest`. Note that all suites other than the built-in `test` suite will by convention work as if `useJUnitJupiter()` was called. You do **not** have to explicitly configure the testing framework on these additional suites, unless you wish to change to another framework. | | **5** | Add a dependency on the production code of the project to the `integrationTest` suite targets. By convention, only the built-in `test` suite will automatically have a dependency on the production code of the project. | | **6** | Configure all targets of this suite. By convention, test suite targets have no relationship to other `Test` tasks. This example shows that the slower integration test suite targets should run after all targets of the `test` suite are complete. | | **7** | Configure the `check` task to depend on all `integrationTest` targets. By convention, test suite targets are not associated with the `check` task. | Invoking the `check` task on the above configured build should show output similar to: \> Task :compileJava \> Task :processResources NO-SOURCE \> Task :classes \> Task :jar \> Task :compileTestJava \> Task :processTestResources NO-SOURCE \> Task :testClasses \> Task :test \> Task :compileIntegrationTestJava \> Task :processIntegrationTestResources NO-SOURCE \> Task :integrationTestClasses \> Task :integrationTest \> Task :check BUILD SUCCESSFUL in 0s 6 actionable tasks: 6 executed Note that the `integrationTest` test suite does not run until after the `test` test suite completes. ## [](#sec:configuring_the_built_in_test_suite)[Configure the built-in `test` suite](#sec:configuring_the_built_in_test_suite) build.gradle ```groovy testing { (1) suites { test { useTestNG() (1) targets { all { testTask.configure { (2) // set a system property for the test JVM(s) systemProperty 'some.prop', 'value' options { (3) preserveOrder = true } } } } } } } ``` | | | | --- | --- | | **1** | Declare the `test` test suite uses the TestNG test framework. | | **2** | Lazily configure the test task of all targets of the suite; note the return type of `testTask` is `TaskProvider`. | | **3** | Configure more detailed test framework options. The `options` will be a subclass of `org.gradle.api.tasks.testing.TestFrameworkOptions`, in this case it is `org.gradle.api.tasks.testing.testng.TestNGOptions`. | ## [](#configure_dependencies_of_a_test_suite)[Configure dependencies of a test suite](#configure_dependencies_of_a_test_suite) build.gradle ```groovy testing { suites { test { (1) dependencies { // Note that this is equivalent to adding dependencies to testImplementation in the top-level dependencies block implementation 'org.assertj:assertj-core:3.21.0' (2) annotationProcessor 'com.google.auto.value:auto-value:1.9' (3) } } } } ``` | | | | --- | --- | | **1** | Configure the built-in `test` test suite. | | **2** | Add the assertj library to the test’s compile and runtime classpaths. The `dependencies` block within a test suite is already scoped for that test suite. Instead of having to know the global name of the configuration, test suites have a consistent name that you use in this block for declaring `implementation`, `compileOnly`, `runtimeOnly` and `annotationProcessor` dependencies. | | **3** | Add the Auto Value annotation processor to the suite’s annotation processor classpath so that it will be run when compiling the tests. | The `dependencies` block within a test suite does not provide access to the same methods as the top-level `dependencies` block. For instance, the [platform()](https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.DependencyHandler.html#org.gradle.api.artifacts.dsl.DependencyHandler:platform%28java.lang.Object%29) and [enforcedPlatform()](https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.DependencyHandler.html#org.gradle.api.artifacts.dsl.DependencyHandler:platform%28java.lang.Object%29) methods are not available. To use these and similar methods, you need to access the top-level dependencies handler directly with `project.dependencies.platform(…​)` or `project.dependencies.enforcedPlatform(…​)`. | | | | --- | --- | | | Though it is discouraged, as suggested by the comment in the snippet above, you can also access the configurations of a test suite in a top-level `dependencies` block after creating the test suite. The configurations used by test suites are normal Gradle configurations, but their exact names should be considered an implementation detail subject to change. For this reason, configuration via the suite’s own `dependencies` block is recommended. | ## [](#configure_source_directories_of_a_test_suite)[Configure source directories of a test suite](#configure_source_directories_of_a_test_suite) build.gradle ```groovy testing { suites { integrationTest(JvmTestSuite) { (1) sources { (2) java { (3) srcDirs = ['src/it/java'] (4) } } } } } ``` | | | | --- | --- | | **1** | Declare and configure a suite named `integrationTest`. The `SourceSet` and synthesized `Test` tasks will be based on this name. | | **2** | Configure the `sources` of the test suite. | | **3** | Configure the `java` SourceDirectorySet of the test suite. | | **4** | Overwrite the `srcDirs` property, replacing the conventional `src/integrationTest/java` location with `src/it/java`. | ## [](#configure_the_type_of_a_test_suite)[Configure the type of a test suite](#configure_the_type_of_a_test_suite) build.gradle ```groovy testing { suites { secondaryTest(JvmTestSuite) { testType = TestSuiteType.INTEGRATION_TEST (1) } } } ``` | | | | --- | --- | | **1** | This suite would use a type value of 'secondary-test' by default. This explicitly sets the type to 'integration-test'. | ## [](#configure_the_test_task_for_a_test_suite)[Configure the `Test` task for a test suite](#configure_the_test_task_for_a_test_suite) build.gradle ```groovy testing { suites { integrationTest { targets { all { (1) testTask.configure { maxHeapSize = '512m' (2) } } } } } } ``` | | | | --- | --- | | **1** | Configure the `integrationTest` task created by declaring a suite of the same name. | | **2** | Configure the `Test` task properties. | `Test` tasks associated with a test suite target can also be configured directly, by name. It is not necessary to configure via the test suite DSL. ## [](#sec:outgoing_variants)[Outgoing Variants](#sec:outgoing_variants) Each test suite creates an outgoing variant containing its test execution results. These variants are designed for consumption by the [Test Report Aggregation Plugin](https://docs.gradle.org/current/userguide/test_report_aggregation_plugin.html#test_report_aggregation_plugin). The attributes will resemble the following. User-configurable attributes are highlighted below the sample. outgoingVariants task output ``` -------------------------------------------------- Variant testResultsElementsForTest (i) -------------------------------------------------- Description = Directory containing binary results of running tests for the test Test Suite's test target. Capabilities - org.gradle.sample:list:1.0.2 (default capability) Attributes - org.gradle.category = verification - org.gradle.testsuite.name = test (1) - org.gradle.testsuite.target.name = test (2) - org.gradle.testsuite.type = unit-test (3) - org.gradle.verificationtype = test-results Artifacts - build/test-results/test/binary (artifactType = directory) ```