laitimes

Software Testing Learning Notes丨JUnit5 enables parallel configuration

author:Tested 666
This article is transferred from the tester community, the original link: jck28-lucio-junit5 to enable parallel configuration [advanced] - study notes - tester community

Environment configuration

<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>           

Use case preparation

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");
    }
}           

No concurrency

  • If there is no concurrency configuration, the main "main" thread runs by default.

Parallel steps

  • Configure the parallel parameter for parallel testing.
    • junit.jupiter.execution.parallel.enabled的值设置为true。
  • Note: Enabling this property is only the first step required to execute tests in parallel.
    • If enabled, ++ test classes and methods will still be executed sequentially by default ++.

parallel configuration

  • Configure it in the JUnit5 configuration file "junit-platform.properties" (the easiest way)
  • junit.jupiter.execution.parallel.enabled=true
  • By providing parameters to the Maven SureFire plugin
  • By providing system properties to the JVM

1. JUnit5 configuration file is created

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

2. Parallel test configuration

  • The following configuration is put into the JUnit5 configuration file:

    junit.jupiter.execution.parallel.enabled=true

3. Run

  • The command line goes to the current project path, enters mvn clean test, and the result is as follows:
Software Testing Learning Notes丨JUnit5 enables parallel configuration

conclusion

  • Tests are run sequentially
  • Use the ForkJoin thread pool

Free video tutorial sharing for software testing and development - Official Account - Tester Community

Software Testing Learning Notes丨JUnit5 enables parallel configuration

Read on