【发布时间】:2019-11-23 20:48:21
【问题描述】:
出于某种原因,我真的很难让显示名称在带有 Kotlin 的 JUnit 5 中真正得到尊重。 这是我为示例而创建的测试文件:
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
@DisplayName("Example Test")
class ExampleTest {
@Test
@DisplayName("test name")
fun myTest() {
Assertions.assertThat(false).isTrue()
}
}
但不是使用这些名称,而是显示实际的类/方法名称,就好像它们根本没有用 @DisplayName 注释一样。这是./gradlew test 的输出:
project.path.ExampleTest > myTest() FAILED
org.opentest4j.AssertionFailedError at ExampleTest.kt:12
25 tests completed, 1 failed
> Task :test FAILED
我一直在想我的 Gradle 配置一定有问题,但是设置非常简单,所以我不知道我可能做错了什么。 这是我的 build.gradle.kts:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.3.60"
id("org.jmailen.kotlinter") version "2.1.2"
id("org.jetbrains.kotlin.plugin.serialization") version "1.3.60"
}
version = "0.1"
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.14.0")
testImplementation("org.junit.jupiter:junit-jupiter:5.5.2")
testImplementation("org.assertj:assertj-core:3.14.0")
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.test {
useJUnitPlatform()
}
在这一点上真的很摸不着头脑,所以任何想法都将不胜感激。它也没有使用我为动态测试提供的名称(与 @TestFactory 一起使用),这特别烦人。
【问题讨论】:
-
我刚刚意识到这可能永远不会在 Gradle 运行器中工作。但它绝对应该在 IntelliJ 中工作,而且它也没有出现在那里。
-
显示名称应该出现在 Gradle 生成的测试 html 报告中,以及 IntelliJ 的 JUnit 视图中(这里有)。请注意,在 Kotlin 中,通常不使用 DisplayName,而是在反引号之间使用方法名称:
@Test fun `test name`() -
@JBNizet 谢谢,它们都没有出现。实际上,我在大多数地方都使用反引号来做这件事,但我仍然希望能够为测试类设置显示名称(用于良好的嵌套)或动态生成的测试,但这些都不起作用。跨度>
-
@JBNizet 哦,我骗了你。它显示在 HTML 报告中,而不是 IntelliJ 中。嗯。
标签: gradle kotlin junit junit5