当然。使用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 ✔