junit junit
JUnit @DisplayName annotation is used to provide a custom name for the test class and test methods. We can use spaces, special characters, and even emojis in the display name.
JUnit @DisplayName批注用于为测试类和测试方法提供自定义名称。 我们可以在显示名称中使用空格,特殊字符,甚至表情符号。
JUnit显示名称示例 (JUnit Display Name Example)
By default, JUnit reporting prints the class name and method name in the IDE test report. We can use @DisplayName to specify a custom name that is easy to read and provide information about the test class and method.
默认情况下, JUnit报告会在IDE测试报告中打印类名和方法名。 我们可以使用@DisplayName来指定一个易于阅读的自定义名称,并提供有关测试类和方法的信息。
Let’s see some examples of JUnit Jupiter @DisplayName annotation.
让我们看一些JUnit Jupiter @DisplayName批注的示例。
Display Name for Test Class
测试类别的显示名称
@DisplayName("MyTestClass")
public class DisplayNameTest {
}
Display Name for Methods
方法的显示名称
@Test
@DisplayName("Example Test Method with No Business Logic")
void test() {
assertTrue(3 > 0);
}
Display Name with Emojis
显示名称和表情符号
@Test
@DisplayName("MyTestMethod ☺")
void test1(TestInfo testInfo) {
assertEquals("MyTestMethod ☺", testInfo.getDisplayName());
}
Notice that we can get the Test method display name in the method by injecting TestInfo
to the method argument.
注意,通过将TestInfo
注入到方法参数中,可以在方法中获得Test方法的显示名称。
报表中的JUnit DisplayName (JUnit DisplayName in Reporting)
When we run our JUnit test class, we can see the display name in the JUnit view window.
运行JUnit测试类时,我们可以在JUnit视图窗口中看到显示名称。
摘要 (Summary)
JUnit Jupiter @DisplayName annotation doesn’t provide any testing benefits. However, it can be used to provide information about the test methods that show in reporting and can be understood easily by any non-technical user too.
JUnit Jupiter @DisplayName注释没有提供任何测试好处。 但是,它可以用来提供有关报告中显示的测试方法的信息,并且任何非技术用户也可以轻松理解。
翻译自: https://www.journaldev.com/21674/junit-display-name-displayname
junit junit