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

一、前言

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

二、Testng

1. 安装

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

2. 使用

1public class UniqueIdGeneratorTest { 2 private Set<Long> ids = new ConcurrentHashSet<>(128); 3 4 @org.testng.annotations.Test(invocationCount = 128, threadPoolSize = 3) 5 public void testGenerateId() { 6 final Long id = UniqueIdGenerator.generateId(); 7 assertTrue(ids.add(id), "id exists," + id); 8 } 9 10}

3. 效果

testng.png

三、Junit5

1. 安装

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

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

1 <dependencies> 2 <!-- test --> 3 <dependency> 4 <groupId>org.testng</groupId> 5 <artifactId>testng</artifactId> 6 <version>${testng.version}</version> 7 <scope>test</scope> 8 </dependency> 9 </dependencies>

2 配置项

3 配置方式

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

4. 使用

1class UniqueIdGeneratorTest2 { 2 private static Set<Long> ids = new ConcurrentHashSet<>(128); 3 4 @org.junit.jupiter.api.RepeatedTest(128) 5 @org.junit.jupiter.api.parallel.Execution(ExecutionMode.CONCURRENT) 6 void generateId() { 7 final Long id = UniqueIdGenerator.generateId(); 8 assertTrue(ids.add(id), "id exists," + id); 9 } 10}

5. 效果

junit5.png

四、总结

本文遵守【CC BY-NC】协议,转载请保留原文出处及本版权声明,否则将追究法律责任。
本文首先发布于 https://www.890808.xyz/ ,其他平台需要审核更新慢一些。

javalover123

点赞
收藏

评论区

加载中...

相关推荐

java junit框架的windows自动化

  在京东混了一个月,基本有点稳定了,觉得也有所余力了现在,继续写博客吧,不过以后更新也许不是那么频繁了  本人使用的是junit框架,对开发是一个单元测试的java框架,但是对测试而言是java的基石之一,与testng差不多平分秋色(好吧,其实是稍微差一点)在上文http://www.cnblogs.com/xuezhezlr/p/77736

Spring Boot API 服务测试指南

SpringBoot除了简化了Spring应用的开发,同时也简化了Spring应用的测试。它内置支持各种常用测试工具,包括SpringTest、JUnit、TestNG、Mockito、AssertJ等。本文将讲解如何编写单元测试和集成测试来保障SpringBootAPI应用不同层级代码的质量,其中会涉及到使用嵌入式的H2数据库

Testng Retry失败用例重新运行的方法(一)

Testng是Java自动化测试的一个框架,它提供了一个对失败用例重新执行的监听器,即接口IRetryAnalyzer。先准备一个测试类:importorg.testng.annotations.Test;importstaticorg.testng.Assert.assertTrue;pu

TestNG+Selenium

是一个开源自动化测试框架。其实类似于JUnit这种单元测试框架,但进行了一些功能扩展属于selenium?还是说TestNG是一个测试框架,它用到了selenium的web自动化测试的功能,比如使用浏览器对应的driver去进行操作,不一定要由RobotFramework触发参考TestNG环境设置(

Twitter的分布式自增ID算法snowflake (Java版)

概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移

JUnit

一、会用Spring测试套件的好处在开发基于Spring的应用时,如果你还直接使用Junit进行单元测试,那你就错过了Spring为我们所提供的饕餮大餐了。使用Junit直接进行单元测试有以下四大不足:1)导致多次Spring容器初始化问题根据JUnit测试方法的调用流程,每执行一个测试方法都会创建一个测试用例的实例并调用setUp()方法。由于