【发布时间】:2018-11-05 23:20:23
【问题描述】:
无论如何我们可以在 Junit 5 中实现动态 DisplayName(例如:用系统属性替换)
@DisplayName("The test cases is running agains {os.name}")
public void testOSVersion(){
.....
}
我们希望这样做是为了使测试用例更具描述性。
谢谢
【问题讨论】:
标签: java unit-testing junit5
无论如何我们可以在 Junit 5 中实现动态 DisplayName(例如:用系统属性替换)
@DisplayName("The test cases is running agains {os.name}")
public void testOSVersion(){
.....
}
我们希望这样做是为了使测试用例更具描述性。
谢谢
【问题讨论】:
标签: java unit-testing junit5
默认情况下是不可能的。
TestReporter使用 当前 Jupiter 版本,您可以始终使用 TestReporter 向报告发送额外数据:
@Test
void testOSVersion(TestReporter testReporter) {
testReporter.publishEntry("os.name", System.getProperty("os.name"));
}
详情见https://junit.org/junit5/docs/current/user-guide/#writing-tests-dependency-injection
DisplayNameGenerator即将推出的 Jupiter 5.4.0 版(已作为 SNAPSHOT 提供)支持名为 @DisplayNameGeneration 的注释,该注释指向自定义的 DisplayNameGenerator 实现。在这里,您可以动态生成测试方法的显示名称,使用它的名称、附加注释等...
详情见 https://junit.org/junit5/docs/snapshot/user-guide/#display-name-generators
【讨论】:
如果其他人像我一样偶然发现这个问题,希望为ParameterizedTests 定制DisplayNames,有有几个可用选项:https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests-display-names。文档中的示例是:
@DisplayName("Display name of container")
@ParameterizedTest(name = "{index} ==> the rank of ''{0}'' is {1}")
@CsvSource({ "apple, 1", "banana, 2", "'lemon, lime', 3" })
void testWithCustomDisplayNames(String fruit, int rank) {
...
}
结果如下:
Display name of container ✔
├─ 1 ==> the rank of 'apple' is 1 ✔
├─ 2 ==> the rank of 'banana' is 2 ✔
└─ 3 ==> the rank of 'lemon, lime' is 3 ✔
【讨论】: