动态测试并发
- 要在 Junit 中启用并行测试,在
src/test/resources
下创建junit-platform.properties
:
junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.config.strategy=dynamic
动态测试并发
- 指定动态测试方法执行并发
- 指定动态测试类执行并发
package com.junit5.dynamictest_parallel;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import java.util.Arrays;
import java.util.Collection;
@Execution(ExecutionMode.CONCURRENT) //方法注解,类注解
public class DynaimcParallelTest {
// @Execution(ExecutionMode.CONCURRENT)
@TestFactory
Collection<DynamicTest> parallelTest1() {
DynamicTest dynamicTest1 = DynamicTest.dynamicTest("1st parallel", () -> {
System.out.println(Thread.currentThread().getName() + "---1st parallel");
});
DynamicTest dynamicTest2 = DynamicTest.dynamicTest("2st parallel", () -> {
System.out.println(Thread.currentThread().getName() + "---2st parallel");
});
DynamicTest dynamicTest3 = DynamicTest.dynamicTest("3st parallel", () -> {
System.out.println(Thread.currentThread().getName() + "---3st parallel");
});
return Arrays.asList(dynamicTest1, dynamicTest2, dynamicTest3);
}
@Execution(ExecutionMode.SAME_THREAD)
@TestFactory
Collection<DynamicTest> parallelTest2() {
DynamicTest dynamicTest1 = DynamicTest.dynamicTest("SAME_THREAD1st parallel", () -> {
System.out.println(Thread.currentThread().getName() + "---SAME_THREAD1st parallel");
});
DynamicTest dynamicTest2 = DynamicTest.dynamicTest("SAME_THREAD2st parallel", () -> {
System.out.println(Thread.currentThread().getName() + "---SAME_THREAD2st parallel");
});
DynamicTest dynamicTest3 = DynamicTest.dynamicTest("SAME_THREAD3st parallel", () -> {
System.out.println(Thread.currentThread().getName() + "---SAME_THREAD3st parallel");
});
return Arrays.asList(dynamicTest1, dynamicTest2, dynamicTest3);
}
}