当前位置: 首页 > 知识库问答 >
问题:

如何使用gradle运行kotlintest测试?

蔚楷
2023-03-14

kotlintest测试从Intellij启动时运行得非常好,但当我尝试使用gradle test task命令运行它们时,只会找到并运行我的常规JUnit测试。

kotlintest代码:

import io.kotlintest.matchers.shouldBe
import io.kotlintest.specs.StringSpec

class HelloKotlinTest : StringSpec() {
    init {
        println("Start Kotlin UnitTest")

        "length should return size of string" {
            "hello".length shouldBe 5
        }
    }
}

build.gradle:

apply plugin: 'org.junit.platform.gradle.plugin'

buildscript {
    ext.kotlinVersion = '1.1.3'
    ext.junitPlatformVersion = '1.0.0-M4'

    repositories {
        maven { url 'http://nexus.acompany.ch/content/groups/public' }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath "org.junit.platform:junit-platform-gradle-plugin:$junitPlatformVersion"
    }
}

sourceSets {
    main.kotlin.srcDirs += 'src/main/kotlin'
    test.kotlin.srcDirs += 'test/main/kotlin'
}

(...) 

dependencies {
    // Kotlin
    compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jre8', version: kotlinVersion

    // Kotlin Test
    testCompile group: 'io.kotlintest', name: 'kotlintest', version: kotlinTestVersion

    // JUnit 5
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junitJupiterVersion
    testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junitJupiterVersion
}

共有3个答案

明财
2023-03-14

为了使用Junit5运行kotlintest测试,您需要使用KTestRunner。

@RunWith(KTestJUnitRunner::class)
class MyTest : FunSpec({
    test("A test") {
        1 + 1 shouldBe 2
    }
})

以下面的gradle配置为例。

dependencies {
    testCompile("io.kotlintest:kotlintest:${kotlinTestVersion}")
    testCompile("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
}
狄海
2023-03-14

“解决方案”是切换回JUnit4。

kotlintest的构建没有考虑JUnit5,也没有提供自己的JUnit引擎。

(注意:应该可以告诉JUnit 5使用用于kotlintest的JUnit 4引擎。如果有人知道怎么做,请在这里添加解决方案。)

彭风华
2023-03-14

在KotlinTest 3.1中。你不需要再使用Junit4了。它与JUnit 5完全兼容。因此,您的问题的答案是升级到3.1。x轨道。

您需要将useJUnitPlatform()添加到build.gradle.中的测试块

您需要添加testCompile'io。kotlintest:kotlintest-runner-junit5:3.1.9'到您的依赖项。

例如。

dependencies {
    testCompile 'io.kotlintest:kotlintest-runner-junit5:3.1.9'
}

test {
    useJUnitPlatform()

    // show standard out and standard error of the test JVM(s) on the console
    testLogging.showStandardStreams = true

    testLogging {
        events "PASSED", "FAILED", "SKIPPED", "STANDARD_OUT", "STANDARD_ERROR"
    }
}
 类似资料:
  • 如何在使用JUnit5的同时使用Gradle Kotlin DSL运行kotlintest测试? 我尝试了什么? 如何使用Gradle运行kotlintest测试?本质上是这个问题的旧版本,但由于使用了不同的技术:JUnit4和Gradle而不是Gradle Kotlin DSL已经过时。我能找到的所有其他问题要么是针对JUnit4,要么是针对没有Kotlintest的JUnit5。 PS在Git

  • 我正在尝试使用Gradle从Kotlintest获取分层测试报告。我看过一些截图,但是我运气不好。对于任何类型的测试(FunSpec、WordSpec、BehaviorSpec等),我总是只看到类名,然后是“叶”测试。 Gradle 5.6.2 Kotlintest 3.4.2 JUnit平台1.5.2 样本测试类 建筑格拉德尔 IntelliJ结果 格雷德尔报告 我需要做什么才能在报告中显示级别

  • 问题内容: 目前,我有以下 build.gradle 文件: 这 的build.gradle 文件是我的仓库 在这里 。我所有的主文件都在 src / model /中 ,它们各自的测试在 test / model中 。 如何正确添加JUnit 4 依赖项 ,然后在 测试/模型 文件夹中运行那些测试? 问题答案: 如何正确添加junit 4依赖关系? 假设您要针对标准Maven(或等效版本)存储库

  • 当前我有以下build.gradle文件: 此build.gradle文件用于我的存储库。我的所有主文件都在src/model/中,它们各自的测试都在test/model中。

  • 我有一个混合的Java/Scala项目,包含JUnit和ScalaTest测试。使用scalatest插件,Gradle在中运行scalatest测试,但忽略中的JUnit测试。没有插件,Gradle运行JUnit测试,但忽略Scala。我错过了什么把戏? 我的: ETA:我知道有可能对Scala测试进行注释,以迫使它们与JUnit测试运行器一起运行。我正在寻找一个一站式的解决方案,它不需要编辑每

  • 我知道怎么说 但如何指定多个? 这似乎只是随机运行一个测试。 这告诉我 在根项目“MyProject”中找不到任务“MyPackage.Model.ModelTest”。 我读了很多手册和教程(和一些SO帖子),但没有找到明确的答案。但是我看到有人通过-tests“somePackage.*”作为参数,所以我想空格周围的引号可能会有帮助。 这不会很快失败,但不幸的是,它只运行列表中的第一个测试。