如何将Gradle中的原生JUnit5支持与Kotlin DSL结合使用?
我想在Gradle Kotlin DSL中使用内置JUnit 5,因为在构建过程中,我收到以下警告:如何将Gradle中的原生JUnit5支持与Kotlin DSL结合使用?,gradle,kotlin,gradle-kotlin-dsl,Gradle,Kotlin,Gradle Kotlin Dsl,我想在Gradle Kotlin DSL中使用内置JUnit 5,因为在构建过程中,我收到以下警告: 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
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不能识别任务本身,即未解析的引用
此外,此问题相关,但要求创建新任务,而不是自定义现有任务:
[2019年4月编辑]在我提出这个问题三个月后,Gradle实际上为Kotlin DSL创建了一个用户指南,可以在
他们还添加了从Groovy到Kotlin的迁移指南
回答:
您要求的语法是
tasks.test {
// Use the built-in JUnit support of Gradle.
useJUnitPlatform()
}
这是我从Kotlin DSL GitHub中找到的,或者你可以使用
tasks.withType<Test> {
useJUnitPlatform()
}
(由于Gradle Kotlin DSL除了一些(未记录的)示例文件之外几乎没有任何文档,因此我在这里记录了一些常见的示例。)
(在中完成示例项目,自我提升…在接受的答案上添加,也可以使用类型化任务配置,如:
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType{
useJUnitPlatform()
}
更新:
Gradle文档供参考。具体而言,示例19具有:
tasks.withType<JavaCompile> {
options.isWarnings = true
// ...
}
tasks.withType{
options.isWarnings=true
// ...
}
嘿,我试过了,你完全正确!即使过了几年,格拉德尔语对我来说仍然很神奇。你能补充一下你在哪里找到这个的答案吗?我很好奇,因为在我写我的答案的时候,我在任何地方都找不到关于它的任何东西。我从Gradle docs推断,我仍然在与Gradle DSL斗争。谢谢链接!我很高兴看到他们在添加文档,因为我问了这个问题(你链接到的文档是从2018年8月开始的,在这个问题三个月后:),而不是tasks{“test”(test::class){useJUnitPlatform()}
我只能说tasks{test{useJUnitPlatform()}
@emanuelgeorgeoategan谢谢,自从我写了这篇文章之后,语法明显改进了,我更新了答案。
tasks.withType<JavaCompile> {
options.isWarnings = true
// ...
}