天天看點

軟體測試學習筆記丨JUnit5開啟并行配置

作者:測試人666
本文轉自測試人社群,原文連結:jck28-lucio-junit5開啟并行配置【進階】 - 學習筆記 - 測試人社群

環境配置

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

用例準備

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

無并發

  • 如果沒有并發配置,預設就是主「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線程池

軟體測試開發免費視訊教程分享 - 公衆号 - 測試人社群

軟體測試學習筆記丨JUnit5開啟并行配置

繼續閱讀