运行 在 kotlin 多平台项目中测试

Running test in kotlin multiplatform project

我已经设置了一个 kotlin 多平台项目并想要 运行 junit 测试。

但是一个

gradle clean build

刚刚交付:

Kotlin Multiplatform Projects are an experimental feature.

BUILD SUCCESSFUL in 1s
9 actionable tasks: 9 executed

这是我的 build.gradle:

buildscript {
    ext.kotlin_version = '1.4.0-rc'
}

plugins {
    id 'org.jetbrains.kotlin.multiplatform' version "$kotlin_version"
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

kotlin {
    jvm {
        withJava()
    }

    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        jvmMain {
            dependencies {
                implementation kotlin('stdlib-jdk8')
            }
        }
        jvmTest {
            dependencies {
                implementation kotlin('test')
                implementation kotlin('test-junit')
                implementation 'io.kotlintest:kotlintest-runner-junit5:3.3.2'
                implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
                implementation "org.junit.jupiter:junit-jupiter-engine:5.5.2"
                implementation "org.junit.jupiter:junit-jupiter-api:5.5.2"
                implementation "org.junit.jupiter:junit-jupiter-params:5.5.2"
            }
        }
    }
}

这是我的测试(位于src/jvmTest/kotlin):

import org.junit.jupiter.api.Test

class JvmTest {
    @Test
    fun testX() {
        println("Hello World")
        println("Hello World")
        println("Hello World")
        println("Hello World")
        println("Hello World")
    }
}

我期待 Hello World 的输出,但如您所见,没有输出。

我必须更改什么,执行测试?还是它被执行了但输出没有显示?我该怎么做才能看到测试的输出?

我也尝试过 kotlin 1.3.72 版。相同的结果。

编辑:我将测试更改为

import junit.framework.TestCase.assertTrue
import org.junit.jupiter.api.Test

class JvmTest {
    @Test
    fun testX() {
        assertTrue(false)
    }
}

相同的结果,构建 运行s 成功,没有执行测试。 build/reports/tests

中没有文件

尝试添加

test {
    testLogging {
        showStandardStreams = true
    }
}

[project]/build/reports/tests 中查找各种 index.html 文件。

此外,要验证测试是否为 运行,使测试失败可能比打印语句更容易。类似于以下内容。

@Test
fun testX() {
    assertTrue(false)
}

我假设您所有的测试都在 jvm 源代码中,因为它们导入了 junit。我还会在 commonTest 源中尝试一个,以确保一切都按预期工作。

添加

tasks.jvmTest{
    useJUnitPlatform()
}

在 build.gradle 中修复了问题。

build.gradle 现在看起来如下:

buildscript {
    ext.kotlin_version = '1.4.0-rc'
}

plugins {
    id 'org.jetbrains.kotlin.multiplatform' version "$kotlin_version"
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

kotlin {
    jvm {
        withJava()
    }

    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib')
                implementation kotlin('stdlib-common')
            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        jvmMain {
            dependencies {
                implementation kotlin('stdlib-jdk8')
            }
        }
        jvmTest {
            dependencies {
                dependsOn commonTest
                implementation kotlin('test')
                implementation kotlin('test-junit')
                implementation 'io.kotlintest:kotlintest-runner-junit5:3.3.2'
                implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
                runtimeOnly "org.junit.jupiter:junit-jupiter-engine:5.5.2"
                implementation "org.junit.jupiter:junit-jupiter-api:5.5.2"
                implementation "org.junit.jupiter:junit-jupiter-params:5.5.2"
            }
        }
    }
}

tasks.jvmTest{
    useJUnitPlatform()
}