【问题标题】:Running test in kotlin multiplatform project在 kotlin 多平台项目中运行测试
【发布时间】:2020-12-02 00:12:17
【问题描述】:

我已经设置了一个 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)
    }
}

同样的结果,构建运行成功,没有执行任何测试。 build/reports/tests 中没有文件

【问题讨论】:

    标签: kotlin kotlin-multiplatform


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 我按你说的改了。测试已构建,但未执行。没有报告文件。
    • 我在 jvm 和 common 部分进行了测试。没有被执行。所以我把它简化为 jvm 测试,首先检查它们,如果它们被执行。之后我会回去看看,如果我可以运行常见的测试。
    【解决方案2】:

    添加

    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()
    }
    

    【讨论】:

    【解决方案3】:

    尝试添加

    test {
        testLogging {
            showStandardStreams = true
        }
    }
    

    【讨论】:

    • 没有任何变化。实际上,似乎仍然没有执行测试。否则必须显示测试结果。
    • 测试不应该在src/main/test吗?
    猜你喜欢
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2020-03-16
    相关资源
    最近更新 更多