一、在test resource文件夹下创建一个名为 junit-platform.properties的文件
The JUnit Platform configuration file: a file named junit-platform.properties in the root of the class path that follows the syntax rules for a Java Properties file.
![](https://img.haomeiwen.com/i13705264/fc6eae6c1d170f45.png)
二、在该文件中配置各项数据
#要启用并行执行需将该参数设置为true 不启用为false
junit.jupiter.execution.parallel.enabled=true
#方法级别的设置 值有concurrent和same_thread 根据自己项目情况设置 二者区别见第三条
junit.jupiter.execution.parallel.mode.default = concurrent
#类级别的设置
junit.jupiter.execution.parallel.mode.classes.default = same_thread
#并发策略设置 值有dynamic、fixed、custom 以下为fixed举例 区别见第四条
junit.jupiter.execution.parallel.config.strategy = fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=3
三、区分SAME_THREAD与CONCURRENT区别
SAME_THREAD简单理解为串行
Force execution in the same thread used by the parent. For example, when used on a test method, the test method will be executed in the same thread as any @BeforeAll or @AfterAll methods of the containing test class.
CONCURRENT简单理解为并行
Execute concurrently unless a resource lock forces execution in the same thread.
以下图解为不同类和方法之间分别设置SAME_THREAD和CONCURRENT的执行顺序
A类中有test1()和test2()
B类中有test1()和test2()
![](https://img.haomeiwen.com/i13705264/938ecb4445602d9d.png)
四、区分dynamic、fixed、custom
1、dynamic:默认选项 在不设置系数的情况下 并行度将等于可用处理器/核的数量
dynamic对应的系数配置项为:junit.jupiter.execution.parallel.config.dynamic.factor
并发线程数为 系数*可用处理器/核的数量
2、fixed:固定线程数设置
搭配为:junit.jupiter.execution.parallel.config.fixed.parallelism
并发线程数为 设置的junit.jupiter.execution.parallel.config.fixed.parallelism的value值
3、custom :自定义
通过实现接口 ParallelExecutionConfigurationStrategy来配置并行的线程池数量
Allows you to specify a custom ParallelExecutionConfigurationStrategy implementation via the mandatory junit.jupiter.execution.parallel.config.custom.class configuration parameter to determine the desired configuration.
五:调用
以注解 @RepeatedTest(n)进行测试case
n:代表case执行的次数
![](https://img.haomeiwen.com/i13705264/631f1a61650964cc.png)
网友评论