'Why isn't @DisplayName working for me in 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()
}
}
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
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()
}
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.
Solution 1:[1]
Finally figured this out. It was an IntelliJ configuration issue. (The display names are never displayed in the command line anyway apparently.)
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"
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.
Solution 2:[2]
It's because IntelliJ uses gradle test runner by default and it does not allow custom display name.
The solution is really simple you just need to change the default test runner from "Gradle" to "IntelliJ Idea".
- Preferences
- Build, Execution, Deployment
- Build Tools
- Gradle
- Run tests using = IntelliJ Idea
Solution 3:[3]
Truth is, for me, after I undid the changes suggested on this post and selected the check icon to the right of the green play button, the description of the tests started to show.
Solution 4:[4]
I managed to get it working in surefirereports by using the configuration in https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html#Surefire_Extensions_and_Reports_Configuration_for_.40DisplayName
it displays output like this "name" is from @DisplayName
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Ersoy |
Solution 3 | |
Solution 4 | user1393631 |