I have the following module structure: ```lib-test...
# community-support
c
I have the following module structure:
Copy code
lib-tests/src/main/kotlin/org/example/ReusableTest.kt
app-impl1/
app-impl2/
In the
dependencies {}
in
build.gradle.kts
of
app-impl1/
and
app-impl2/
I have
testImplementation(project(":lib-tests"))
. The purpose is, as I hope is obvious from the structure, to have the tests from
lib-tests/
run for both apps. However, the tests are not run. I'm using Gradle 8.7, Java 21, Kotlin 1.9.23, JUnit Platform 5.10.2. Where can I find more documentation on how Gradle discovers tests so that I can get this running? (Note that for feature files in Cucumber running as JUnit extension, this setup works just fine, pulling them in from
lib-tests/src/main/resources/features
.)
t
The
Test
task only looks at its
testClassesDirs
property for where to find test classes (https://docs.gradle.org/current/userguide/java_testing.html#sec:java_testing_basics), so if the test classes come from another project I suppose you'd have to first copy/extract them into a directory you can configure as
testClassesDirs
.
c
So effectively adding
../lib-tests/build/classes/kotlin/main
to
testClassesDirs
should do the trick?
Yup. Adding this did the trick:
Copy code
tasks.withType<Test> {
    useJUnitPlatform()
    testClassesDirs = testClassesDirs.plus(files("../lib-tests/build/classes/kotlin/main"))
}
Is there a nicer way to do that? It doesn't feel right to me to hardcode the path to the build outputs of another module.
@Thomas Broyer Anyway, this was already incredibly helpful. Thank you. Can I buy you a coffee?
t
Off the top of my head: create a configuration, configure it to resolve the
classes
variant (ask for the libraryelements=classes attribute, maybe also add artifact-type=directory) and non-transitive, add
project(":lib-tests")
as a dependency to that configuration, then add the configuration to the
testClassesDirs
.
(I think you'd still have to add the project as a
testImplementation
dependency as well)