创建动态测试的规则
- 不能是
privateor
,static
。
- 返回类型必须为:
Stream、Collection、Iterable、或Iterator
、DynamicNode
。
返回类型
DynamicNode
- Stream
- Collection
- Iterable
- Iterator
返回DynamicNode
- 返回DynamicTest
- 返回DynamicContainer
package com.junit5.createdynamictest;
import org.junit.jupiter.api.*;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DynamicBaseTest {
// Static test 1
@Test
@DisplayName("加法静态测试用例")
void test_Add() {
assertEquals(5, 3+2);
}
// Static test 2
@Test
@DisplayName("除法静态测试用例")
void test_Devide() {
assertEquals(5,25/5);
}
@TestFactory
@DisplayName("动态测试用例")
Collection<DynamicNode> dynamicForCollecion(){
DynamicTest dynamicTest1 = DynamicTest.dynamicTest("加法动态测试用例", () -> {
System.out.println("这是一个加法动态测试用例");
assertEquals(5, 3 + 2);
});
DynamicTest dynamicTest2 = DynamicTest.dynamicTest("除法动态测试用例", () -> {
System.out.println("这是一个除法动态测试用例");
assertEquals(5,25/5);
});
return Arrays.asList(dynamicTest1,dynamicTest2);
}
}
Stream
package com.junit5.dynamictest_rule;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.PrimitiveIterator;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DynamicNodeTest {
@TestFactory
@DisplayName("单个动态测试")
DynamicTest dynamicTest() {
return DynamicTest.dynamicTest("返回DynamicTest", () -> {
assertEquals(5, 3 + 2);
});
}
@TestFactory
@DisplayName("容器动态测试")
DynamicContainer dynamicContainer() {
return DynamicContainer
.dynamicContainer("多个动态测试的容器",
Stream.of(
DynamicTest.dynamicTest("加法动态测试用例", () -> {
System.out.println("这是一个加法动态测试用例");
assertEquals(5, 3 + 2);
}),
DynamicTest.dynamicTest("除法动态测试用例", () -> {
System.out.println("这是一个除法动态测试用例");
assertEquals(5, 25 / 5);
})
)
);
}
//5,6,7 是不是比2大
@TestFactory
@DisplayName("动态测试单个Stream流")
Stream<DynamicTest> dynamicTestStream() {
return Stream.of(5, 6, 7).map(
arg ->
DynamicTest.dynamicTest("动态测试-" + arg, () -> {
System.out.println("参数:" + arg);
assertThat(arg, is(greaterThan(2)));
})
);
}
//5,6,7 是不是比2大
@TestFactory
@DisplayName("动态测试多个Stream流")
Stream<DynamicContainer> dynamicTestStreams() {
return Stream.of(5, 6, 7).map(
arg -> DynamicContainer.dynamicContainer("Container-" + arg,
Stream.of(
DynamicTest.dynamicTest("1动态测试:" + arg, () -> {
System.out.println("参数:" + arg);
assertThat(arg, is(greaterThan(2)));
}),
DynamicTest.dynamicTest("2动态测试:" + arg, () -> {
System.out.println("参数:" + arg);
assertThat(arg, is(lessThan(9)));
})
)
)
);
}
}
Collection
package com.junit5.dynamictest_rule;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.PrimitiveIterator;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DynamicNodeTest {
@TestFactory
@DisplayName("动态测试Collection")
Collection<DynamicTest> dynamicTestCollection(){
Collection<DynamicTest> dynamicTests = new ArrayList<>();
DynamicTest dynamicTest1=DynamicTest.dynamicTest("add-动态测试",()->{
System.out.println("add动态测试用例:");
});
DynamicTest dynamicTest2=DynamicTest.dynamicTest("sub-动态测试",()->{
System.out.println("sub动态测试用例:");
});
dynamicTests.add(dynamicTest1);
dynamicTests.add(dynamicTest2);
return dynamicTests;
}
}
迭代器
-
Iterator
- 具有迭代状态的对象
- 有
hasNext()
/next()
-
Iterable
- 是一系列可以迭代的元素 > Java中集合使用.iterator() 返回可迭代的对象
package com.junit5.dynamictest_rule;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.PrimitiveIterator;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DynamicNodeTest {
//返回类型为迭代器
@TestFactory
@DisplayName("迭代器动态测试")
Iterator<DynamicTest> dynamicTestIterator(){
Collection<DynamicTest> dynamicTests= new ArrayList<>();
PrimitiveIterator.OfInt iterator = IntStream.iterate(2, n -> n + 1).limit(3).iterator();
while (iterator.hasNext()){
Integer next = iterator.next();
DynamicTest dynamicTest1=DynamicTest.dynamicTest("n-动态测试",()->{
System.out.println("n:"+next);
assertThat(next,is(greaterThan(1)));
});
DynamicTest dynamicTest2=DynamicTest.dynamicTest("2.n+1-动态测试",()->{
int a=next+1;
a+=1;
System.out.println("n2:"+a);
assertThat(a,is(greaterThan(2)));
});
dynamicTests.add(dynamicTest1);
dynamicTests.add(dynamicTest2);
}
return dynamicTests.iterator();
}
@TestFactory
@DisplayName("迭代器的动态测试2")
Iterable<DynamicTest> dynamicTestIterable(){
Collection<DynamicTest> dynamicTests= new ArrayList<>();
PrimitiveIterator.OfInt iterator = IntStream.iterate(2, n -> n + 1).limit(3).iterator();
while (iterator.hasNext()){
Integer next = iterator.next();
DynamicTest dynamicTest1=DynamicTest.dynamicTest("迭代器n-动态测试",()->{
System.out.println("n:"+next);
assertThat(next,is(greaterThan(1)));
});
DynamicTest dynamicTest2=DynamicTest.dynamicTest("迭代器n2.n+1-动态测试",()->{
int a=next+1;
a+=1;
System.out.println("n2:"+a);
assertThat(a,is(greaterThan(2)));
});
dynamicTests.add(dynamicTest1);
dynamicTests.add(dynamicTest2);
}
return dynamicTests;
}
}