Tested on Java LTS versions from 8 to 21.
Tested on Gradle versions from 6.0 to 8.12.
Usage:
plugins {
id 'name.remal.test-source-sets' version '4.1.9'
}
A Gradle plugin that provides testSourceSets
extension for creating new source sets for testing. For all created source sets a Test task is created. All dependencies are inherited from test
source set.
testSourceSets
extension is very similar to sourceSet
extension on Gradle Java plugin. It can be used in exactly the same way. A new source set integrationTest
for integration tests can be defined like this:
testSourceSets {
integrationTest
}
After executing it, sourceSets
will contain these source sets:
main
test
integrationTest
(this source set is added here, because creation of test source sets is done by delegating it tosourceSet
extension)
And testSourceSets
will contain these source sets:
test
integrationTest
All configurations of test source sets extend corresponding configurations of test
source set.
So, after defining integrationTest
test source set:
testSourceSets {
integrationTest
}
... you'll get these configurations:
integrationTestCompileOnly
(extendstestCompileOnly
)integrationTestImplementation
(extendstestImplementation
)integrationTestRuntimeOnly
(extendstestRuntimeOnly
)- And so on. You can find all source set configurations by search for
get*ConfigurationName()
methods of SourceSet.
Test tasks creation
Test task is created by the plugin for each test source set.
These Test tasks do not inherit main test
task settings, so you need to configure it explicitly, or by using withType()
:
tasks.withType(Test).configureEach {
useJUnitPlatform()
}
Starting from Gradle 7.3, this plugin applies JVM Test Suite plugin.
Creating a new test source set, creates a new JVM test suite.
Gradle authors believe that it's important to distinguish test sources from production sources. So, it's expected that a source set for tests ends with Test
. See an issue about naming.
JVM Test Suite plugin creates a Test task named the same, as the corresponding test source set. So, by having integration
test source set, we'll have integration
Test task. Which is confusing.
This plugin checks that all test source set names end with Test
. By default, an exception is thrown if it's not so.
This check can be configured:
testSourceSets {
testSuffixCheck = 'FAIL' // to throw an exception, default
testSuffixCheck = 'WARN' // to log a warning message
testSuffixCheck = 'DISABLE' // to disable the check
}
A task named allTests
is created by the plugin. This task simply depends on Test task of each test source set.
Every test source set (including default test
) has testTask
extension.
This extension has type TaskProvider<Test>
and it's a provider of corresponding Test
task for the current test source set.
It can be used like this:
testSourceSets.configureEach {
println testTask.name // prints corresponding name of `Test` task for this source set
}
Internal members of main
source set are accessible in all test source sets. It works for Kotlin Gradle plugin >=1.3.60. The way it's done is described here.
Usage:
plugins {
id 'name.remal.integration-tests' version '4.1.9'
}
A Gradle plugin that applies name.remal.test-source-sets
plugin and creates integrationTest
test source set.
testTaskName
convention property is not added anymore to each test source set. Test tasks now have their names equal to test source set names.
name.remal.integration-tests
plugin creates integrationTest
test source set instead of integration
.
Package name was changed from name.remal.gradleplugins.testsourcesets
to name.remal.gradle_plugins.test_source_sets
.