gradle - 如何将 Gradle 中的原生 JUnit 5 支持与 Kotlin DSL 结合使用?

标签 gradle kotlin gradle-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 中,您可以只写任务的名称,然后更改 block 中的内容,但 Kotlin DSL 无法识别任务本身,未解析的引用。

另外,这个问题是相关的,但要求创建任务,而不是自定义现有任务:How do I overwrite a task in gradle kotlin-dsl

Here is a solution for normal Gradle.

最佳答案

[2019 年 4 月编辑] 作为 Pedro has found ,在我提出这个问题三个月后,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()
}

我从 this example file 中发现的来自 Kotlin DSL GitHub,或者您可以使用

tasks.withType<Test> {
    useJUnitPlatform()
}

用于this official userguide这是在写完这个答案几个月后创建的(感谢 Pedro's answer 注意到这一点)。

但无论如何,您实际上仍在使用 buildscript block ,它本身有点被弃用,请改用新的 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 除了一些(未记录的)示例文件 on GitHub 之外几乎没有任何文档,我在这里记录了一些常见的示例。)

(在 GitHub 上的完整示例项目, self 推销...)

关于gradle - 如何将 Gradle 中的原生 JUnit 5 支持与 Kotlin DSL 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50128728/

相关文章:

jenkins - 在 jenkins 管道脚本中获取 gradle 变量

gradle - Grails 梯度 "a task with that name already exists"

android - 如何高效查询包含两个WHERE子句的表(Android Room)

kotlin - 来自类型参数的属性的自定义 getter

android - 任何处理器都无法识别 Kapt 选项

android - Gradle kotlin-dsl 配置不起作用,无法识别 android 扩展功能

android - Android Studio 0.8.2-Gradle异常?

android - 迁移到 androidX - 无法导入/查找 androidx.databinding.DatabindingUtil

kotlin - 以 Kotlin 方式获取包含在字符串中的子字符串的索引

kotlin - 使用 Gradle (Kotlin DSL) 导入源依赖项