3

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/
│  │  ├─ common/
│  │  │  ├─ kotlin/
│  │  │  ├─ resources/
│  │  ├─ unit/
│  │  │  ├─ kotlin/
│  │  │  ├─ resources/
│  │  ├─ integration/
│  │  │  ├─ kotlin/
│  │  │  ├─ resources/
│  │  ├─ functional/
│  │  │  ├─ kotlin/
│  │  │  ├─ resources/
│  │  ├─ performance/
│  │  │  ├─ 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/common/kotlin")
        }
        resources {
            srcDir("src/test/common/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/unit/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/unit/kotlin", sourceSetCommon.java.srcDirs))
                }
                resources {
                    setSrcDirs(listOf("src/test/unit/resources"))
                    // setSrcDirs(listOf("src/test/unit/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/functional/kotlin"))
                }
                resources {
                    setSrcDirs(listOf("src/test/functional/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 (unit) directory and functional test (functional) directory. However, this does not work as expected. Any thoughts/input are greatly appreciated!

tbcrawford
  • 387
  • 1
  • 8
  • 23

2 Answers2

0

Maybe my solution could help you.

My test structure is as follows:

src/test/common
src/test/unitTest
src/test/integrationTest
src/test/acceptanceTest

In fact, I only adjusted the default test source set to become common, moved its sources from src/test to src/test/common, and made the other test source sets dependent on it.

Here is all you need to make it working (in Kotlin):

plugins {
    `jvm-test-suite`
}

testing {
    suites {
        getByName<JvmTestSuite>("test") {
            testType.set("common")
            useJUnitJupiter()
            sources {
                kotlin {
                    setSrcDirs(listOf("src/test/common/kotlin"))
                }
                resources {
                    setSrcDirs(listOf("src/test/common/resources"))
                }
            }
            dependencies {
                // We can replace direct dependency on main's runtimeClasspath with implementation(project())
                // once https://github.com/gradle/gradle/issues/25269 is resolved
                implementation(sourceSets.main.get().runtimeClasspath)
            }
        }

        register<JvmTestSuite>("unitTest") {
            testType.set(UNIT_TEST)
            useJUnitJupiter()
            sources {
                kotlin {
                    setSrcDirs(listOf("src/test/unitTest/kotlin"))
                }
                resources {
                    setSrcDirs(listOf("src/test/unitTest/resources"))
                }
            }
            dependencies {
                implementation(sourceSets.test.get().runtimeClasspath)
                implementation(sourceSets.test.get().output)
            }
        }

        register<JvmTestSuite>("integrationTest") {
            testType.set(INTEGRATION_TEST)
            useJUnitJupiter()
            sources {
                kotlin {
                    setSrcDirs(listOf("src/test/integrationTest/kotlin"))
                }
                resources {
                    setSrcDirs(listOf("src/test/integrationTest/resources"))
                }
            }
            dependencies {
                implementation(sourceSets.test.get().runtimeClasspath)
                implementation(sourceSets.test.get().output)
            }
        }

        register<JvmTestSuite>("acceptanceTest") {
            testType.set("acceptance-test")
            useJUnitJupiter()
            sources {
                kotlin {
                    setSrcDirs(listOf("src/test/acceptanceTest/kotlin"))
                }
                resources {
                    setSrcDirs(listOf("src/test/acceptanceTest/resources"))
                }
            }
            dependencies {
                implementation(sourceSets.test.get().runtimeClasspath)
                implementation(sourceSets.test.get().output)
            }
        }
    }
}
tasks.named("check") {
    dependsOn(
        testing.suites.named("unitTest"),
        testing.suites.named("integrationTest"),
        testing.suites.named("acceptanceTest")
    )
}
Michal
  • 113
  • 8
0

I ended up with a much simpler solution which you can see below.

In this case, I create a new source set called commonTest. Then for each JvmTestSuite type, I add as a dependency: the project and commonTest's output. Since I added the extension methods at the top, this makes configuration for each test set, and any future ones, extremely easy to add.

plugins {
    `jvm-test-suite`
}

fun SourceSet.configureSrcSetDirs(dirName: String) {
    java.setSrcDirs(listOf("src/test/$dirName/kotlin"))
    resources.setSrcDirs(listOf("src/test/$dirName/resources"))
}

fun JvmTestSuite.shouldRunAfter(vararg paths: Any) =
    targets.all {
        testTask.configure {
            shouldRunAfter(paths)
        }
    }

fun SourceSet.addSrcSetMainClasspath() {
    compileClasspath += sourceSets.main.get().compileClasspath + sourceSets.main.get().output
    runtimeClasspath += output + compileClasspath
}

val commonTest: SourceSet by sourceSets.creating {
    addSrcSetMainClasspath()
    configureSrcSetDirs("common")
}

testing {
    suites {
        configureEach {
            if (this is JvmTestSuite) {
                dependencies {
                    implementation(project(path))
                    implementation(commonTest.output)
                }
                sources.addSrcSetMainClasspath()
            }
        }

        val test by getting(JvmTestSuite::class) {
            testType.set(TestSuiteType.UNIT_TEST)
            sources.configureSrcSetDirs("unit")
        }

        val functionalTest by registering(JvmTestSuite::class) {
            testType.set(TestSuiteType.FUNCTIONAL_TEST)
            sources.configureSrcSetDirs("functional")
            shouldRunAfter(test)
        }

        val integrationTest by registering(JvmTestSuite::class) {
            testType.set(TestSuiteType.INTEGRATION_TEST)
            sources.configureSrcSetDirs("integration")
            shouldRunAfter(functionalTest)
        }

        register<JvmTestSuite>("performanceTest") {
            testType.set(TestSuiteType.PERFORMANCE_TEST)
            sources.configureSrcSetDirs("performance")
            shouldRunAfter(integrationTest)
        }
    }
}

val testImplementation: Configuration by configurations.getting

val integrationTestImplementation: Configuration by configurations.getting {
    extendsFrom(testImplementation)
}

val functionalTestImplementation: Configuration by configurations.getting {
    extendsFrom(testImplementation)
}

val performanceTestImplementation: Configuration by configurations.getting {
    extendsFrom(testImplementation)
}

tasks.named("check") {
    // already depends on "test"
    dependsOn(testing.suites.named("functionalTest"))
    dependsOn(testing.suites.named("integrationTest"))
    dependsOn(testing.suites.named("performanceTest"))
}

tbcrawford
  • 387
  • 1
  • 8
  • 23