Using the jvm-test-suite
gradle plugin, I would like to be able to create a common test source set for use in other test suites. I envision the structure to look like the following where the sources and resources from common
can be used in unit
, integration
, functional
, and performance
:
project/
├─ src/
│ ├─ main/
│ ├─ test/
│ │ ├─ commonTest/
│ │ │ ├─ kotlin/
│ │ │ ├─ resources/
│ │ ├─ unitTest/
│ │ │ ├─ kotlin/
│ │ │ ├─ resources/
│ │ ├─ integrationTest/
│ │ │ ├─ kotlin/
│ │ │ ├─ resources/
│ │ ├─ functionalTest/
│ │ │ ├─ kotlin/
│ │ │ ├─ resources/
│ │ ├─ performanceTest/
│ │ │ ├─ kotlin/
│ │ │ ├─ resources/
So far I have tried the following, which I thought would provide the proper classpaths for each test suite:
@file:Suppress("UnstableApiUsage")
plugins {
`jvm-test-suite`
}
// Register `commonTest` source set
sourceSets {
register("commonTest") {
java {
compileClasspath += named("main").get().output
runtimeClasspath += named("main").get().output
srcDir("src/test/commonTest/kotlin")
}
resources {
srcDir("src/test/commonTest/resources")
}
}
}
// Make `commonTestImplementation` extend from `testImplementation` so that we can use all dependencies that `testImplementation` uses
val commonTestImplementation by configurations.getting {
extendsFrom(configurations.named("testImplementation").get())
}
configure<TestingExtension> {
suites {
val sourceSetMain = sourceSets.named("main").get()
val sourceSetCommon = sourceSets.named("commonTest").get()
// These might be able to just be variables instead of lazy evaluation
val sourceSetMainClasspath = { sourceSetMain.compileClasspath + sourceSetMain.output }
val sourceSetCommonClasspath = { sourceSetMain.compileClasspath + sourceSetMain.output }
val test by getting(JvmTestSuite::class) {
testType.set(TestSuiteType.UNIT_TEST)
sources {
// Add common test compile classpath and outputs to the `unitTest` suite?
compileClasspath += sourceSetCommonClasspath()
runtimeClasspath += output + compileClasspath
java {
setSrcDirs(listOf("src/test/unitTest/kotlin"))
// I've also tried the following which only works when applied to only 1 test suite but not all. Same with the commented out resources portion directly below
// setSrcDirs(listOf("src/test/unitTest/kotlin", sourceSetCommon.java.srcDirs))
}
resources {
setSrcDirs(listOf("src/test/unitTest/resources"))
// setSrcDirs(listOf("src/test/unitTest/resources", sourceSetCommon.resources.srcDirs))
}
}
}
val functionalTest by registering(JvmTestSuite::class) {
testType.set(TestSuiteType.FUNCTIONAL_TEST)
dependencies {
implementation(project())
}
sources {
// Add common test compile classpath and outputs to the `unitTest` suite?
compileClasspath += sourceSetCommonClasspath()
runtimeClasspath += output + compileClasspath
java {
setSrcDirs(listOf("src/test/functionalTest/kotlin"))
}
resources {
setSrcDirs(listOf("src/test/functionalTest/resources"))
}
}
targets {
all {
testTask.configure {
shouldRunAfter(test)
}
}
}
}
}
}
val functionalTestImplementation by configurations.getting {
extendsFrom(configurations.named("testImplementation").get())
}
From this, I expect to be able to access common test sources in both the unit test (unitTest
) directory and functional test (functionalTest
) directory. However, this does not work as expected. Any thoughts/input are greatly appreciated!