如何将 Gradle 中的原生 JUnit 5 支持与 Kotlin DSL 一起使用?
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()
}
(因为这道题下面有些废话'contains mostly code')。
我试图找到有关如何在 Kotlin DSL 中自定义任务的文档,但找不到任何文档。在正常情况下 Groovy 您可以只写任务的名称,然后在块中更改内容,但 Kotlin DSL 无法识别任务本身,未解决的引用。
此外,这个问题是相关的,但要求创建 new 任务,而不是自定义现有任务:
[编辑 2019 年 4 月] 作为 , three months after I asked this question Gradle actually created a user guide for the Kotlin DSL which can be visited at 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 this example file 中得出的 GitHub,或者您可以使用
tasks.withType<Test> {
useJUnitPlatform()
}
在 this official userguide which was created a couple of months after this answer was written (thanks to 中用于说明这一点)。
但无论如何您实际上仍在使用 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 除了几个(未记录的)示例文件 on GitHub 几乎没有任何文档,我在这里记录了一些常见示例。)
(在 GitHub 完成示例项目,自我推销...)
除了已接受的答案之外,还可以使用类型化任务配置,例如:
tasks.withType<Test> {
useJUnitPlatform()
}
更新:
Gradle 文档供参考 here。具体来说,示例 19 具有:
tasks.withType<JavaCompile> {
options.isWarnings = true
// ...
}
我想将内置的 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()
}
(因为这道题下面有些废话'contains mostly code')。 我试图找到有关如何在 Kotlin DSL 中自定义任务的文档,但找不到任何文档。在正常情况下 Groovy 您可以只写任务的名称,然后在块中更改内容,但 Kotlin DSL 无法识别任务本身,未解决的引用。
此外,这个问题是相关的,但要求创建 new 任务,而不是自定义现有任务:
[编辑 2019 年 4 月] 作为
他们还在 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 this example file 中得出的 GitHub,或者您可以使用
tasks.withType<Test> {
useJUnitPlatform()
}
在 this official userguide which was created a couple of months after this answer was written (thanks to
但无论如何您实际上仍在使用 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 除了几个(未记录的)示例文件 on GitHub 几乎没有任何文档,我在这里记录了一些常见示例。)
(在 GitHub 完成示例项目,自我推销...)
除了已接受的答案之外,还可以使用类型化任务配置,例如:
tasks.withType<Test> {
useJUnitPlatform()
}
更新:
Gradle 文档供参考 here。具体来说,示例 19 具有:
tasks.withType<JavaCompile> {
options.isWarnings = true
// ...
}