我想将内置 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
最佳答案
[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/