Reputation: 377
I have a project with multiple modules. One of the modules is kotlin multiplatform. I'm trying to add unit tests for it using Kotest 4.1.1. This release is published in repository mavenCentral(), and I can use this release just fine in a module that is using the java library plugin. But every attempt to sync when using Kotest 4.1.1 in the multiplatform module fails:
Could not resolve io.kotest:kotest-runner-junit5-jvm:4.1.1.
I can't see why this is happening. This error is definitely specific to the module using the kotlin multiplatform plugin, as a different module in the same project using the regular kotlin jvm plugin and the same release of kotest can sync just fine. So gradle is definitely capable of finding the release in the defined repositories and using kotest 4.1.1.
As a result (no surprise), the kotest stuff is not on the test classpath for commonTest in the multiplatform module, so the unit test classes cannot import the io.kotest.xxx packages.
I was hoping to use kotest in JVM-run unit tests (using junit5) to test the multiplatform code. But this is my first attempt at doing multiplatform, so everything is suspect :-)
The snippet below is the sourceSets code from the multiplatform module's build.gradle.kts. If the commented part is left as-is, gradle syncs fine but source in commonTest of course doesn't see kotest. If the comments are removed, the error occurs
sourceSets {
commonMain {
dependencies {
implementation(kotlin("stdlib-common"))
}
}
/*
commonTest {
dependencies {
implementation(Kotest.framework)
}
}
*/
}
Here's the value of the Kotest.framework val, used in both modules:
object Kotest {
const val version = "4.1.1"
const val framework = "io.kotest:kotest-runner-junit5-jvm:$version"
const val assertions = "io.kotest:kotest-assertions-core-jvm:$version"
const val propertyTest = "io.kotest:kotest-property-jvm:$version"
}
Has anyone gotten kotest 4.1.1 to work when using the kotlin multiplatform plugin in Android Studio? Any ideas about how to diagnose this further would be appreciated. Thanks in advance...
Config: Android studio 4.0 Gradle 6.5.1 Kotlin 1.3.72
Upvotes: 1
Views: 2323
Reputation: 711
The problem with the configuration in question is that a JVM-only artifact (io.kotest:kotest-runner-junit5-jvm
) appears as a dependency in the multiplatform commonTest
section. This would not work as the common sections cover not just the JVM target, but others (JS, Native) as well (see the multiplatform doc).
So the Junit5 runner should go into the JVM backend section only. The common section can include artifacts intended for all targets. See Kotest's Quick Start documentation (select the rightmost tab ("Multiplatform") in each section).
These are the relevant sections from a current build.gradle.kts
script which uses kotest 4.4.1:
val kotestVersion = "4.4.1"
kotlin {
jvm("backend") {
compilations.all {
kotlinOptions.useIR = true
}
withJava()
}
js("frontend", LEGACY) {
browser {
binaries.executable()
testTask {
useKarma {
useChromeHeadless()
webpackConfig.cssSupport.enabled = true
}
}
}
}
sourceSets {
val commonTest by getting {
dependencies {
implementation("io.kotest:kotest-framework-engine:$kotestVersion")
implementation("io.kotest:kotest-assertions-core:$kotestVersion")
implementation("io.kotest:kotest-property:$kotestVersion")
}
}
val backendTest by getting {
dependencies {
implementation("io.kotest:kotest-runner-junit5:$kotestVersion")
}
}
}
}
tasks {
// Tests
withType<Test> {
useJUnitPlatform()
}
}
Note: The above example uses backend
and frontend
names for JVM and JS targets. If you would stick with the default names instead, the backendTest
source set would be called jvmTest
.
Upvotes: 2