gpt4 book ai didi

testing - 使用 @TestFactory 创建测试层次结构

转载 作者:行者123 更新时间:2023-11-28 21:03:55 48 4
gpt4 key购买 nike

我正在使用 Junit 5 注释 @TestFactory 来生成几个这样的测试:

@TestFactory
public Collection<DynamicTest> myTest() throws IOException {
return fetchSomeTests().stream()
.map(test -> {
return dynamicTest(test.get("testDescription"), () -> doMyTest(test));
}).collect(Collectors.toList());
}

是否可以分组组织生成的测试,就像使用不同类的 @Test 一样?

最佳答案

当然。使用 Collection<DynamicNode>作为返回类型并创建任意数量的组。

复制自:https://junit.org/junit5/docs/current/user-guide/#writing-tests-dynamic-tests

DynamicContainer实例由显示名称和动态子节点列表组成,支持创建动态节点的任意嵌套层次结构。

这是生成嵌套动态容器和测试的示例:

@TestFactory
Stream<DynamicNode> dynamicTestsWithContainers() {
return Stream.of("A", "B", "C")
.map(input -> dynamicContainer("Container " + input, Stream.of(
dynamicTest("not null", () -> assertNotNull(input)),
dynamicContainer("properties", Stream.of(
dynamicTest("length > 0", () -> assertTrue(input.length() > 0)),
dynamicTest("not empty", () -> assertFalse(input.isEmpty()))
))
)));
}

它会产生一棵像这样的树:

│  ├─ DynamicTestsDemo ✔
│ │ ├─ dynamicTestsWithContainers() ✔
│ │ │ ├─ Container A ✔
│ │ │ │ ├─ not null ✔
│ │ │ │ └─ properties ✔
│ │ │ │ ├─ length > 0 ✔
│ │ │ │ └─ not empty ✔
│ │ │ ├─ Container B ✔
│ │ │ │ ├─ not null ✔
│ │ │ │ └─ properties ✔
│ │ │ │ ├─ length > 0 ✔
│ │ │ │ └─ not empty ✔
│ │ │ └─ Container C ✔
│ │ │ ├─ not null ✔
│ │ │ └─ properties ✔
│ │ │ ├─ length > 0 ✔
│ │ │ └─ not empty ✔

关于testing - 使用 @TestFactory 创建测试层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49025029/

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