【问题标题】:Running tests in parallel with maven surefire plugin与 maven surefire 插件并行运行测试
【发布时间】:2021-05-16 02:59:43
【问题描述】:

最近得到了一份新工作,我正在从事一个 Maven 和 Micronaut 项目 - 我是 Micronaut 的新手,如果你不熟悉,它是 Spring 的替代品。

以此为指导:https://www.baeldung.com/maven-junit-parallel-tests

我正在尝试让我们的集成测试并行运行,以加快我们的测试速度。我已将所需的配置添加到 pom.xml 文件中。我已经调整了很多,使用不同的设置组合(包括我在其他 SO 帖子中找到的设置)。

但是当我运行测试时,无论有没有这个添加的配置,运行它们所花费的时间是完全相同的 - 1 分 38 秒。所以我认为它不起作用。虽然不知道我在控制台上的预期输出是否正常工作?

在测试中,没有一个使用@RunWith,它使用默认值,应该没问题。

这是pom。有人可以建议吗?谢谢。

<profiles>
    <profile>
      <id>integration</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <excludedGroups>unit, performance</excludedGroups>
              <parallel>classes</parallel>
              <threadCount>10</threadCount>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>performance</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <excludedGroups>unit, integration</excludedGroups>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>all</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <excludedGroups>none</excludedGroups>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
<build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${maven-surefire-plugin.version}</version>
        <configuration>
          <excludedGroups>integration, performance</excludedGroups>
          <parallel>classes</parallel>
          <threadCount>10</threadCount>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>

【问题讨论】:

    标签: java testing automated-tests maven-3 maven-surefire-plugin


    【解决方案1】:

    您能否尝试使用配置选项forkCount

    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <configuration>
          ...
          <forkCount>4</forkCount>
          ...   
       </configuration>
    </plugin>
    

    来自官方的xml元素描述

    用于指定并行分叉的 VM 数量以执行测试的选项。当以“C”结束时, 数字部分乘以 CPU 核心数。浮点值只接受与 “C”。如果设置为“0”,则不会派生任何 VM,并且所有测试都在主进程中执行。

    示例值:“1.5C”、“4”

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题并尝试了以下每种组合:

      1. maven-surefire-plugin(版本 2.22.0)

      2. maven-failsafe-plugin(2.22.0 版)

        • 这是另一个插件,基本相同,但设计用于运行集成测试,因为它不会在第一次失败的测试后停止。
      3. 你提到的parallel/threadCount配置。

      4. https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven-config-paramshttps://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution 指定了这些不同的插件配置。就像这样:

           <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-failsafe-plugin</artifactId>
             <version>2.22.0</version>
             <configuration>
               <properties>
                 <configurationParameters>
                   junit.jupiter.execution.parallel.enabled=true
                   junit.jupiter.execution.parallel.mode.default=same_thread
                   junit.jupiter.execution.parallel.mode.classes.default=concurrent
                 </configurationParameters>
               </properties>
             </configuration>
          </plugin>
        

      唯一有效的组合是 2(maven-failsafe-plugin)和 4(configurationParameters)。

      【讨论】:

        最近更新 更多