关于java:Testng和Junit5多线程并发测试对比

1次阅读

共计 3256 个字符,预计需要花费 9 分钟才能阅读完成。

一、前言

  • 最近测试一个开源我的项目,发现生成的 全局 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. 应用

  • @Test 就能够指定 执行次数 (invocationCount)、并发 (threadPoolSize),很不便
  • 同一个测试对象,ids 属性 不必加 static

    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. 成果

三、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 配置项

  • 先设置 junit.jupiter.execution.parallel.enabled=true
  • 全局并发测试:junit.jupiter.execution.parallel.mode.default=concurrent
  • 部分并发测试:@RepeatedTest 和 @Execution(CONCURRENT)
  • 按需配置并行策略 strategy,倡议 fixed,线程数 可控
  • fixed 配置:如 fixed.parallelism=3,fixed.max-pool-size=3(默认 256 + parallelism)
  • dynamic 策略 (依据 factor、CPU 核数 调整 parallelism),parallelism = max(1, factor * CPU 核数),前面和 fixed 逻辑一样

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. 应用

  • 需改为 @RepeatedTest,和 一般测试不统一
  • 测试对象 不同,ids 属性 需加 static

    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. 成果

四、总结

  • 多线程并发测试 场景,Testng 可能比 Junit5 更适合
  • 应用不多也能够试试 hutool 的 ThreadUtil#concurrencyTest(int threadSize, Runnable runnable)

本文恪守【CC BY-NC】协定,转载请保留原文出处及本版权申明,否则将查究法律责任。
本文首先公布于 https://www.890808.xyz/,其余平台须要审核更新慢一些。

正文完
 0