本文转自测试人社区,原文链接: https://ceshiren.com/t/topic/28207

环境配置

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>11</java.version>
        <!-- 使用 Java 11 语言特性 ( -source 11 ) 并且还希望编译后的类与 JVM 11 ( -target 11 )兼容,您可以添加以下两个属性,它们是默认属性插件参数的名称-->
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
        <!-- 对应junit Jupiter的版本号;放在这里就不需要在每个依赖里面写版本号,导致对应版本号会冲突-->
        <junit.jupiter.version>5.8.2</junit.jupiter.version>

        <maven.compiler.version>3.8.1</maven.compiler.version>
        <maven.surefire.version>3.0.0-M5</maven.surefire.version>
        <!-- plugins -->
        <maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
        <poi.version>5.2.2</poi.version>
    </properties>

    <!--    物料清单 (BOM)-->
    <dependencyManagement>
        <dependencies>
            <!--当使用 Gradle 或 Maven 引用多个 JUnit 工件时,此物料清单 POM 可用于简化依赖项管理。不再需要在添加依赖时设置版本-->
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>${junit.jupiter.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <!--            对应添加的依赖的作用范围-->
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
<!--            <version>${junit.jupiter.version}</version>-->
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
<!--                    <includes>-->
<!--                        <include>top/testeru/group/*_Test.class</include>-->
<!--                    </includes>-->
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>${junit.jupiter.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                        <version>${junit.jupiter.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.

用例准备

public class Parallel1_Test {
    @Test
    void test1(){
        System.out.println(Thread.currentThread().getName()+" => Parallel1_Test--test1");
    }
    @Test
    void test2(){
        System.out.println(Thread.currentThread().getName()+" => Parallel1_Test--test2");
    }
    @Test
    void test3(){
        System.out.println(Thread.currentThread().getName()+" => Parallel1_Test--test3");
    }
}
public class Parallel2_Test {
    @Test
    void test1(){
        System.out.println(Thread.currentThread().getName()+" => Parallel2_Test--test1");
    }
    @Test
    void test2(){
        System.out.println(Thread.currentThread().getName()+" => Parallel2_Test--test2");
    }
    @Test
    void test3(){
        System.out.println(Thread.currentThread().getName()+" => Parallel2_Test--test3");
    }
}
public class Parallel3_Test {
    @Test
    void test1(){
        System.out.println(Thread.currentThread().getName()+" => Parallel3_Test--test1");
    }

    @Test
    void test2(){
        System.out.println(Thread.currentThread().getName()+" => Parallel3_Test--test2");
    }

    @Test
    void test3(){
        System.out.println(Thread.currentThread().getName()+" => Parallel3_Test--test3");
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.

无并发

  • 如果没有并发配置,默认就是主「main」线程运行。

并行步骤

  • 配置并行测试的parallel参数。
  • junit.jupiter.execution.parallel.enabled的值设置为true
  • 注意:启用此属性只是并行执行测试所需的第一步。
  • 如果启用,默认情况下++测试类和方法仍将按顺序执行++。

parallel配置

  • JUnit5的配置文件「junit-platform.properties」进行配置 (最简单的方法)
    junit.jupiter.execution.parallel.enabled=true
  • 通过向 maven surefire 插件提供参数
  • 通过向 JVM 提供系统属性

1.JUnit5配置文件创建

  • 在项目的src/test/resources目录下创建JUnit5的配置文件:junit-platform.properties

2.并行测试配置

  • 以下配置放入JUnit5配置文件中:
    junit.jupiter.execution.parallel.enabled=true

3.运行

  • 命令行进入到当前项目路径,输入mvn clean test,运行结果如下:

软件测试学习笔记丨JUnit5开启并行配置_软件测试

结论

  • 测试按顺序运行
  • 使用ForkJoin线程池