gpt4 book ai didi

java - 在 JUnit 5 中为 @ParameterizedTest 生成显示名称

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:25 34 4
gpt4 key购买 nike

我有一堆 @ParameterizedTest@MethodSource 接收参数,toString() 结果非常冗长(例如 Selenium 的 网络驱动程序)。这些默认用于组成相应的显示名称。来自JUnit 5 user guide :

By default, the display name of a parameterized test invocation contains the invocation index and the String representation of all arguments for that specific invocation. However, you can customize invocation display names via the name attribute of the @ParameterizedTest annotation […]

虽然这允许在一定程度上自定义显示名称,但我似乎无法调整各个参数的字符串表示形式。不幸的是,通过 @DisplayNameGeneration 指定一个生成器只能应用于类级别,不会影响参数化测试调用的显示名称。

有没有办法使用 DisplayNameGenerator@ParameterizedTest 或自定义给定参数的字符串表示?

最佳答案

从 JUnit 5.8.0 开始,有一个 Named<T>作为 JUnit Jupiter API 的一部分的接口(interface),具有“自动支持将包含的有效负载 [参数] 直接注入(inject)参数化方法”(参见 issue #2301)。示例:

@DisplayName("A parameterized test with named arguments")
@ParameterizedTest
@MethodSource("namedArguments")
void testWithNamedArguments(File file) {}

static Stream<Arguments> namedArguments() {
return Stream.of(
Arguments.of(Named.of("An important file", new File("path1"))),
Arguments.of(Named.of("Another file", new File("path2")))
);
}

如果您更喜欢静态导入,您也可以从 Arguments 中寻找相应的别名和 Named :

arguments(named("An important file", new File("path1")))

更多信息请引用corresponding docs .

关于java - 在 JUnit 5 中为 @ParameterizedTest 生成显示名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57892989/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com