簡體   English   中英

如何在 Kotlin DSL 中使用 Gradle 中的原生 JUnit 5 支持?

[英]How do I use the native JUnit 5 support in Gradle with the Kotlin DSL?

我想將內置 JUnit 5 與 Gradle Kotlin DSL 一起使用,因為在構建過程中我收到以下警告:

WARNING: The junit-platform-gradle-plugin is deprecated and will be discontinued in JUnit Platform 1.3.
Please use Gradle's native support for running tests on the JUnit Platform (requires Gradle 4.6 or higher):
https://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle

那個鏈接告訴我把

test {
    useJUnitPlatform()
}

在我的build.gradle中,但是build.gradle.kts的語法是什么?

我當前的構建文件是

import org.gradle.api.plugins.ExtensionAware

import org.junit.platform.gradle.plugin.FiltersExtension
import org.junit.platform.gradle.plugin.EnginesExtension
import org.junit.platform.gradle.plugin.JUnitPlatformExtension

group = "com.example"
version = "0.0"

// JUnit 5
buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.junit.platform:junit-platform-gradle-plugin:1.2.0")
    }
}

apply {
    plugin("org.junit.platform.gradle.plugin")
}

// Kotlin configuration.
plugins {

    val kotlinVersion = "1.2.41"

    application
    kotlin("jvm") version kotlinVersion
    java // Required by at least JUnit.

    // Plugin which checks for dependency updates with help/dependencyUpdates task.
    id("com.github.ben-manes.versions") version "0.17.0"

    // Plugin which can update Gradle dependencies, use help/useLatestVersions
    id("se.patrikerdes.use-latest-versions") version "0.2.1"

}

application {
    mainClassName = "com.example.HelloWorld"
}

dependencies {
    compile(kotlin("stdlib"))
    // To "prevent strange errors".
    compile(kotlin("reflect"))
    // Kotlin reflection.
    compile(kotlin("test"))
    compile(kotlin("test-junit"))

    // JUnit 5
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.2.0")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.2.0")
    testRuntime("org.junit.platform:junit-platform-console:1.2.0")

    // Kotlintest
    testCompile("io.kotlintest:kotlintest-core:3.1.0-RC2")
    testCompile("io.kotlintest:kotlintest-assertions:3.1.0-RC2")
    testCompile("io.kotlintest:kotlintest-runner-junit5:3.1.0-RC2")

}

repositories {
    mavenCentral()
    mavenLocal()
    jcenter()
}

(以下是一些廢話,因為這個問題“主要包含代碼”)。 我試圖在 Kotlin DSL 中查找有關如何自定義任務的文檔,但我找不到任何文檔。 在正常的 Groovy 中,您可以只寫任務的名稱,然后更改塊中的內容,但是 Kotlin DSL 無法識別任務本身,未解決的參考。

此外,這個問題是相關的,但要求創建任務,而不是自定義現有任務: How do I overwrite a task in gradle kotlin-dsl

這是普通 Gradle 的解決方案。

[編輯 2019 年 4 月] 正如Pedro 發現的那樣,在我問這個問題三個月后,Gradle 實際上為 Kotlin DSL 創建了一個用戶指南,可以在https://docs.gradle.org/current/userguide/kotlin_dsl.html訪問

他們還在https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/添加了從 Groovy 到 Kotlin 的遷移指南

回答:

您要求的語法是

tasks.test {
    // Use the built-in JUnit support of Gradle.
    useJUnitPlatform()
}

這是我從 Kotlin DSL GitHub 的示例文件中找出的,或者您可以使用

tasks.withType<Test> {
    useJUnitPlatform()
}

這是在編寫此答案幾個月后創建的官方用戶指南中使用的(感謝Pedro 的回答指出了這一點)。

但無論如何,您實際上仍在使用buildscript塊,它本身有點過時,請改用新的plugins DSL ( docs )。 新的build.gradle.kts變成

group = "com.example"
version = "0.0"

plugins {

    val kotlinVersion = "1.2.41"

    application
    kotlin("jvm") version kotlinVersion
    java // Required by at least JUnit.

    // Plugin which checks for dependency updates with help/dependencyUpdates task.
    id("com.github.ben-manes.versions") version "0.17.0"

    // Plugin which can update Gradle dependencies, use help/useLatestVersions
    id("se.patrikerdes.use-latest-versions") version "0.2.1"
}

application {
    mainClassName = "com.example.HelloWorld"
}

dependencies {
    compile(kotlin("stdlib"))
    // To "prevent strange errors".
    compile(kotlin("reflect"))
    // Kotlin reflection.
    compile(kotlin("test"))
    compile(kotlin("test-junit"))

    // JUnit 5
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.2.0")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.2.0")
    testRuntime("org.junit.platform:junit-platform-console:1.2.0")

    // Kotlintest
    testCompile("io.kotlintest:kotlintest-core:3.1.0-RC2")
    testCompile("io.kotlintest:kotlintest-assertions:3.1.0-RC2")
    testCompile("io.kotlintest:kotlintest-runner-junit5:3.1.0-RC2")

}

repositories {
    mavenCentral()
    mavenLocal()
    jcenter()
}

tasks {
    // Use the native JUnit support of Gradle.
    "test"(Test::class) {
        useJUnitPlatform()
    }
}

(由於 Gradle Kotlin DSL 幾乎沒有任何文檔,除了GitHub 上的一些(未記錄的)示例文件,我在這里記錄了一些常見的示例。)

(完整的示例項目在GitHub 上,自我宣傳...)

除了已接受的答案之外,還可以使用類型化任務配置,例如:

tasks.withType<Test> {
    useJUnitPlatform()
}

更新:

參考搖籃文檔在這里 具體示例 19具有:

tasks.withType<JavaCompile> {
    options.isWarnings = true
    // ...
}

到目前為止,這對我有用...

plugins {
    kotlin("jvm") version "1.7.10"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(kotlin("test"))
    testImplementation("org.junit.jupiter:junit-jupiter:5.9.0")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.0")
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM