Multiplatform: Running kotlin.test Common tests and io.kotlintest JVM tests

I have some tests in my commonTest directory that use the built-in kotlin.test library, and some other tests in jvmTest that use io.kotlintest. I start out with a fairly vanilla build.gradle file.

If I run the jvmTest task, it executes my commonTest tests but not my jvmTest tests.

If I go back and add jvmTest.useJUnitPlatform() to my build.gradle, then run jvmTest again, it executes my jvmTest tests, but not my commonTest tests.

Basically it looks like there’s no way to configure build.gradle such that my commonTest and jvmTests run in the same execution.

Thoughts?

To make it work you need to make the jvmTest sourceSet depend on the commonTest sourceSet. In addition you will need to add the junit implementation to the dependencies for jvmTest (the same is true for any other platform you may want to run the common tests on). Some example of how I’ve implemented it is here:

Thanks…I tried adding dependsOn commonTest to my jvmTest.dependencies block, but unfortunately this seemed not to work for me. The junit implementation was already present in jvmTest:

jvmTest {
    dependencies {
        dependsOn commonTest
        implementation kotlin('test')
        implementation kotlin('test-junit5')
        implementation 'io.kotlintest:kotlintest-runner-junit5:3.3.2'
    }
}

Alright, this works for me:

jvmTest {
    dependencies {
        implementation kotlin('test')
        implementation kotlin('test-junit5')
        implementation 'io.kotlintest:kotlintest-runner-junit5:3.3.2'
        runtimeOnly "org.junit.jupiter:junit-jupiter-engine:5.5.2"
    }
}

Seems it wasn’t working because there was no (junit) TestEngine to pick up the commonTest-style tests. Not sure how that was ever working, even before kotlintest. Anyway, this works :slight_smile:

I have similar problems with running tests in multiplatform project. Documentation is very poor I cannot figure out where is the problem :frowning: – Would appreciate any help.

I made a test project for illustration (attached)

KotlinMultiplatformTestProblems.zip (57.0 KB)

Here are the main pieces:

build.gradle for ModuleA:

plugins {
id ‘org.jetbrains.kotlin.multiplatform’
}

kotlin {
jvm{
}.compilations.all {
kotlinOptions.noReflect = false
kotlinOptions.noStdlib = false
}

sourceSets {
    commonMain {
        dependencies {
            implementation kotlin('stdlib')
            implementation kotlin('stdlib-common')
        }
    }
    commonTest {
        dependsOn commonMain
        dependencies {
            implementation kotlin('test-common')
            implementation kotlin('test-annotations-common')
        }
    }
    jvmMain {
        dependencies {
            implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
        }
    }

    jvmTest {
        dependsOn commonTest
        dependencies {
            implementation kotlin('test')
            implementation kotlin('test-junit5')
            implementation 'io.kotlintest:kotlintest-runner-junit5:3.3.2'
            runtimeOnly "org.junit.jupiter:junit-jupiter-engine:5.5.2"
        }
    }
}

This is what I am getting:


Testing started at 14:44 …

Configure project :
Kotlin Multiplatform Projects are an experimental feature.

The Kotlin source set commonTest was configured but not added to any Kotlin compilation. You can add a source set to a target’s compilation by connecting it with the compilation’s default source set using ‘dependsOn’.
See https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#connecting-source-sets

Task :ModuleA:cleanJvmTest
Task :ModuleA:compileKotlinJvm UP-TO-DATE
Task :ModuleA:jvmProcessResources NO-SOURCE
Task :ModuleA:jvmMainClasses UP-TO-DATE
Task :ModuleA:compileTestKotlinJvm UP-TO-DATE
Task :ModuleA:jvmTestProcessResources NO-SOURCE
Task :ModuleA:jvmTestClasses UP-TO-DATE

Task :ModuleA:jvmTest FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:ModuleA:jvmTest’.

No tests found for given includes: [module_a.SomeClassTest.testSomeFun](filter.includeTestsMatching)

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 327ms
4 actionable tasks: 2 executed, 2 up-to-date

And again answering my own question: this change in jvmTest piece solved my problem:

jvmTest {
            dependencies {
                implementation kotlin('test-junit')
            }
        }

(the problem was 5 in ‘test-junit5’)