写点什么

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

作者:测试人
  • 2024-07-01
    北京
  • 本文字数:2713 字

    阅读完需:约 9 分钟

本文转自测试人社区,原文链接: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>
复制代码

用例准备

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,运行结果如下:


结论

  • 测试按顺序运行

  • 使用 ForkJoin 线程池

软件测试开发免费视频教程分享


发布于: 刚刚阅读数: 5
用户头像

测试人

关注

专注于软件测试开发 2022-08-29 加入

霍格沃兹测试开发学社,测试人社区:https://ceshiren.com/t/topic/22284

评论

发布
暂无评论
软件测试学习笔记丨JUnit5开启并行配置_软件测试_测试人_InfoQ写作社区