为什么@DisplayName在JUnit 5中对我不起作用? [英] Why isn't @DisplayName working for me in JUnit 5?

查看:298
本文介绍了为什么@DisplayName在JUnit 5中对我不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我真的很难让显示名称真正在Kotlin的JUnit 5中得到尊重. 这是我出于示例目的创建的测试文件:

For some reason, I'm really having a hard time getting display names to actually be respected in JUnit 5 with Kotlin. Here's a test file I created for the purpose of example:

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

But instead of these names being used, it's showing the actual class/method name as if they weren't annotated with @DisplayName at all. Here's the output from ./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:

I keep thinking there must be something wrong with my Gradle configuration, but the setup is pretty simple so I don't know what I could be doing wrong. Here's my 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结合使用),这特别令人讨厌.

Really scratching my head at this point, so any ideas would be appreciated. It's not using the names I give to my dynamic tests either (in conjunction with @TestFactory), which is particularly annoying.

推荐答案

最后弄清楚了这一点.这是一个IntelliJ配置问题. (显示名称显然永远不会在命令行中显示.)

Finally figured this out. It was an IntelliJ configuration issue. (The display names are never displayed in the command line anyway apparently.)

结果是我将其配置为使用Gradle测试运行器而不是IntelliJ,后者不显示自定义显示名称.解决方案是进入IntelliJ设置->构建,执行,部署-> Gradle,然后在使用...运行测试"下选择"IntelliJ IDEA"

Turns out I had it configured to use the Gradle test runner instead of the IntelliJ one, which doesn't show custom display names. The solution was to go into IntelliJ settings -> Build, Execution, Deployment -> Gradle and under "Run tests using" select "IntelliJ IDEA"

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

Thanks go to JBNizet for pointing out that display names are supposed to show up in the HTML test report that Gradle generates but not in the command line, which helped me determine that this was an IntelliJ-specific issue.

这篇关于为什么@DisplayName在JUnit 5中对我不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆