Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit Reports -- Test Method Descriptions

Tags:

junit

report

I am trying to see if there is a way to include "descriptive text" in my junit reports by way of javadocs. JUnit 4 doesnt seem to support the 'description' attribute for the @Test annotation like TestNG does.

So far from what I have researched there is only one tool out there called javadoc-junit (http://javadoc-junit.sourceforge.net/). However I could not get this to work since it seems to be incompatible with Junit 4.

What I want is some way to provide a sentence or two of text with my each test method in the JUnit report. JavaDoc is no good since the target audience will have to swtich between JavaDoc and the Junit Report to see documentation and/or test stats.

Anyone know of anything else I could use with minimal effort?

Best, Ray J


1 Answers

In JUnit 5 there is a way to annotate every test with a @DisplayName. The declared test classes can have text, special characters and emojis.

The declared text on each test is visible by test runners and test reports.


The Javadoc says:

public @interface DisplayName

@DisplayName is used to declare a custom display name for the annotated test class or test method. Display names are typically used for test reporting in IDEs and build tools and may contain spaces, special characters, and even emoji.

And the User Guide:

import org.junit.gen5.api.DisplayName;
import org.junit.gen5.api.Test;

@DisplayName("A special test case")
class DisplayNameDemo {

    @Test
    @DisplayName("Custom test name containing spaces")
    void testWithDisplayNameContainingSpaces() {
    }

    @Test
    @DisplayName("╯°□°)╯")
    void testWithDisplayNameContainingSpecialCharacters() {
    }

    @Test
    @DisplayName("😱")
    void testWithDisplayNameContainingEmoji() {
    }
}
like image 125
Patrick Avatar answered Sep 12 '25 16:09

Patrick