Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gradle 为什么@DisplayName在JUnit5中不适合我?_Gradle_Kotlin_Junit_Junit5 - Fatal编程技术网

Gradle 为什么@DisplayName在JUnit5中不适合我?

Gradle 为什么@DisplayName在JUnit5中不适合我?,gradle,kotlin,junit,junit5,Gradle,Kotlin,Junit,Junit5,出于某种原因,我真的很难让显示名在JUnit5中与Kotlin一起得到尊重。 以下是我为示例目的创建的测试文件: 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("t

出于某种原因,我真的很难让显示名在JUnit5中与Kotlin一起得到尊重。 以下是我为示例目的创建的测试文件:

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测试的输出:

我一直在想我的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结合使用,这尤其令人讨厌。

最终解决了这个问题。这是一个IntelliJ配置问题。显示名称永远不会显示在命令行中

事实证明,我已经将它配置为使用Gradle测试运行程序而不是IntelliJ运行程序,IntelliJ运行程序不显示自定义显示名称。解决方案是使用select IntelliJ IDEA进入IntelliJ设置->构建、执行、部署->渐变和运行测试


感谢JBNizet指出显示名称应该显示在Gradle生成的HTML测试报告中,而不是显示在命令行中,这帮助我确定这是IntelliJ特有的问题。

这是因为IntelliJ默认使用Gradle test runner,并且不允许自定义显示名称

解决方案非常简单,您只需将默认测试运行程序从Gradle更改为IntelliJ Idea

偏好 构建、执行、部署 构建工具 格拉德尔 使用=IntelliJ Idea运行测试
我刚刚意识到这可能在Gradle runner中不起作用。但是它肯定应该在IntelliJ中工作,而且它也不会出现在那里。显示名称应该出现在Gradle生成的测试html报告中,在IntelliJ的JUnit视图中也会出现。请注意,在Kotlin中,通常不使用DisplayName,并在backticks之间使用方法名:@Test-fun`Test-name`@JBNizet谢谢,它们都没有显示。实际上,我在大多数地方都在使用反勾号,但我仍然希望能够为测试类设置显示名称,以便进行良好的嵌套,或者使用动态生成的测试,但这些都不起作用。@JBNizet哦,我骗了你。它显示在HTML报告中,而不是IntelliJ中。嗯。对我来说,我有Intellj,但还是看不到显示器Name@SanJaisy只要确保重新启动Intellij,更改就可以生效。非常感谢Ryan的这篇文章。我对此感到非常沮丧,因为它仍然不适合我。我正在用Kotlin编写动态测试,可以在HTML报告中看到显示名称,但在IntelliJ中仍然看不到。我确实更改了设置并重新启动了IDE。还有其他想法吗?
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()
}