Testng和Junit5多线程并发测试对比

一、前言

  • 最近测试一个开源项目,发现生成的 全局id 有重复,也没有单元测试,就准备贡献个 PR
  • 想到多线程并发测试,根据经验,第一想法是用 Testng,后面看了下 Junit5 也有实验性支持了,就对比下(以 maven 为例)
  • spock 不太适合这种场景

二、Testng

1. 安装

  • 选择 使用数 比较多、也比较新 的版本,7.7.1。<testng.version>7.7.1</testng.version>
  • 多模块项目,可以在 根pom.xml里面添加依赖,避免每个模块都增加配置哦
    <dependencies>
        <!-- test -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2. 使用

public class UniqueIdGeneratorTest {
    private Set<Long> ids = new ConcurrentHashSet<>(128);

    @org.testng.annotations.Test(invocationCount = 128, threadPoolSize = 3)
    public void testGenerateId() {
        final Long id = UniqueIdGenerator.generateId();
        assertTrue(ids.add(id), "id exists," + id);
    }

}

3. 效果

testng.png

三、Junit5

1. 安装

  • 选择 使用数 比较多、也比较新 的版本,5.8.2。<junit-jupiter.version>5.8.2</junit-jupiter.version>
  • 最好通过 dependencyManagement 来统一版本,尤其是 多模块项目
  • 建议放到 spring-boot-dependencies 前面,优先级更高
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>${junit-jupiter.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>    </dependencyManagement>

多模块项目,可以在 根pom.xml里面添加依赖,避免每个模块都增加配置哦

    <dependencies>
        <!-- test -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

2 配置项

3 配置方式

  • System properties 配置方式,更适合多模块项目(根pom.xml配置,子模块就不用配置了)
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <argLine>-Djunit.jupiter.execution.parallel.enabled=true -Djunit.jupiter.execution.parallel.config.strategy=fixed
                        -Djunit.jupiter.execution.parallel.config.fixed.parallelism=3 -Djunit.jupiter.execution.parallel.config.fixed.max-pool-size=3
                    </argLine>
                </configuration>
            </plugin>
  • 配置文件
    test/resources 目录下,增加 junit-platform.properties 文件,内容如下:
#是否允许并行执行true/false
junit.jupiter.execution.parallel.enabled=true
#是否支持方法级别多线程same_thread/concurrent
junit.jupiter.execution.parallel.mode.default=concurrent
#是否支持类级别多线程same_thread/concurrent
junit.jupiter.execution.parallel.mode.classes.default=concurrent
# the maximum pool size can be configured using a ParallelExecutionConfigurationStrategy
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=3
junit.jupiter.execution.parallel.config.fixed.max-pool-size=3

4. 使用

class UniqueIdGeneratorTest2 {
    private static Set<Long> ids = new ConcurrentHashSet<>(128);

    @org.junit.jupiter.api.RepeatedTest(128)
    @org.junit.jupiter.api.parallel.Execution(ExecutionMode.CONCURRENT)
    void generateId() {
        final Long id = UniqueIdGenerator.generateId();
        assertTrue(ids.add(id), "id exists," + id);
    }
}

5. 效果

junit5.png

四、总结

本文遵守【CC BY-NC】协议,转载请保留原文出处及本版权声明,否则将追究法律责任。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Junit5测试框架 一、Junit5框架构成 与以前的 JUnit 版本不同,JUnit 5 由来自三个不同子项...
    羋学僧阅读 450评论 0 1
  • 概述 编写的测试代码就是 Java 代码 运行要遵守 Java 的语法和 JVM 的规定 测试类型 手工测试 单元...
    bowen_wu阅读 251评论 0 0
  • 关于JUnit5,前辈们的文章写的都很好,比如:一篇教你学会Junit5[https://zhuanlan.zhi...
    伊丽莎白2015阅读 932评论 0 0
  • JUnit 5 作为新一代的 Java 单元测试框架,提供很多改进。例如对比 JUnit4 与 JUnit5 的官...
    MrTT阅读 12,617评论 0 7
  • 一、概念 JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vin...
    一只夜猫子阅读 1,258评论 0 0