https://kotlinlang.org logo
#gradle
Title
# gradle
g

Gleb Minaev

03/18/2023, 11:11 AM
Is there a way to mark custom Kotlin source set as a "test source set" in IDEA via Gradle? I know that there is a way to mark standard Gradle source set (via
idea { module { testSources.from(<path to the source directory>) } }
). But Kotlin source sets are not standard Gradle source set... (Here is the use case.) I have a project with separate benchmarks source set. (Which is used in kotlinx.benchmark.) And benchmark tasks are marked as test ones. But the source set is not. So I want to "fix" the difference.
a

Adam S

03/18/2023, 12:01 PM
is your project Kotlin/JVM or Multiplatform?
g

Gleb Minaev

03/18/2023, 1:11 PM
Kotlin/Multiplatform. But soltion for Kotlin/JVM are appreciated too.
a

Adam S

03/18/2023, 1:12 PM
for JVM there's the Test Suite plugin - it works really well. You can make a separate test suite for your benchmarks, and IntelliJ will understand it https://docs.gradle.org/current/userguide/jvm_test_suite_plugin.html
and, in case you didn't know, if you find that you need to make some test utils that you want to share between all of the test suites, then you'll want the java-test-fixtures plugin https://docs.gradle.org/current/userguide/java_testing.html#sec:java_test_fixtures You can even share test-utils between subprojects It's JVM only - for Kotlin Multiplatform test utils it's probably easier to make a separate 'test utils' subproject.
g

Gleb Minaev

03/18/2023, 1:26 PM
Yeah, I didn't know about that. Thank you very much!
a

Adam S

03/18/2023, 1:30 PM
for a separate source set in Kotlin/Multiplatform, you can try the following: 1. create a new source set, e.g.
commonBenchmarkTests
, that depends on
commonTest
2. create a new platform specific source set, e.g.
jvmBenchmarkTests
, that depends on the common benchmark source set, and
jvmMain
3. tell Kotlin that the JVM target should compile the
jvmBenchmarkTests
source set
Copy code
kotlin {
    jvm()

    sourceSets {
        val commonMain by getting { }
        val commonTest by getting { }

        val jvmMain by getting { }
        val jvmTest by getting { }

        val commonBenchmarkTests by creating {
            description = "Common benchmarks"
            dependsOn(commonMain)
        }

        val jvmBenchmarkTests by creating {
            description = "JVM benchmarks"
            dependsOn(jvmMain)
            dependsOn(commonBenchmarkTests)
        }

        jvm {
            compilations["test"].source(jvmBenchmarkTests)
        }
    }
}
IntelliJ seems to mark the new source sets as 'test', but I didn't try and compile it.
77 Views