junit junit_JUnit显示名称– @DisplayName

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注释没有提供任何测试好处。 但是,它可以用来提供有关报告中显示的测试方法的信息,并且任何非技术用户也可以轻松理解。

GitHub Repository. GitHub Repository中检出完整的类和其他JUnit示例。

翻译自: https://www.journaldev.com/21674/junit-display-name-displayname

junit junit

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值