排除在jUnit中并行运行的特定测试

时间:2014-07-17 18:58:12

标签: java maven parallel-processing junit4

我最近偶然发现了一种通过jUnit并行执行测试的简单方法,方法是在java项目的pom.xml文件中指定以下内容:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <configuration>
  <parallel>classes</parallel>
 </configuration>
</plugin>

我发现有2个测试类(我们称之为“badtestclass1”和“badtestclass2”),由于编写测试的方式不断受到并行执行的惩罚。理想情况下,我会重构那些测试类以表现得更好,但在此期间,我想知道是否有一种漂亮的方法来“排除”这些特定类并行执行。基本上,是否有一种方法可以并行执行其他所有操作,然后按顺序执行其他操作(或其他顺序,无关紧要)。会有类似以下的工作吗?

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <configuration>
  <parallel>classes</parallel>
  <excludes>
   <excludesFile>badtestclass1</excludesFile>
   <excludesFile>badtestclass2</excludesFile>
  </excludes>
 </configuration>
</plugin>

3 个答案:

答案 0 :(得分:9)

您可以使用jcip @NotThreadSafe注释您不希望并行化的类,并保留您在启动示例中的确保配置。这样,只要surefire找到带注释的类,它就会在单个线程中执行它。它在&#34; 并行测试执行和单线程执行&#34;中正确解释here。段落。

答案 1 :(得分:3)

排除原始测试短语中的那两个测试,然后在单线程中运行这两个类创建一个新的执行? :)

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>path/to/your/class/badtestclass1.java</exclude>
            <exclude>path/to/your/class/badtestclass2.java</exclude>
        </excludes>
        <parallel>classes</parallel>
    </configuration>

    <executions>
        <execution>
            <id>single-thread-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <includes>
                    <include>path/to/your/class/badtestclass1.java</include>
                    <include>path/to/your/class/badtestclass2.java</include>
                </includes>
                <threadCount>1</threadCount>
            </configuration>
        </execution>
    </executions>
  </plugin>

答案 2 :(得分:1)

我在这里参加聚会有点晚了,尝试了前面的两个答案,在某些情况下可以使用,但不能提供完整的解决方案。可能相关,我正在使用JUnit 5。

@polaretto的answer建议使用jcip @NotThreadSafe批注,该批注可与构建中的surefire插件一起使用,但不适用于命令行mvn test。 @ patson-luk的answer处在正确的轨道上,不幸的是,通过排除默认配置中的“不良测试”,它被排除在外,并且未在单独的<execution>中运行。

我设法通过以下配置使它工作:

对于JUnit 5,这些就足够了

在我的src/main/resources/junit-platform.properties文件中:

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent

在我的非线程安全类的顶部(而不是jcip注释):

import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD;

@Execution(SAME_THREAD)
class SingleThreadedTest {
   // ...
}

对于JUnit 4,修改接受的答案,如下所示:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>${maven-surefire-plugin.version}</version>
  <configuration>
    <!-- Skip default, define executions separately below -->
    <skip>true</skip>
  </configuration>
  <executions>
    <execution>
      <id>single-thread-test</id>
      <phase>test</phase>
      <goals>
        <goal>test</goal>
      </goals>
      <configuration>
        <!-- Not thread safe, run separately -->
        <includes>
          <include>**/SingleThreadedTest.java</include>
        </includes>
        <forkCount>1</forkCount>
        <reuseForks>false</reuseForks>
        <threadCount>1</threadCount>
        <skip>false</skip>
      </configuration>
    </execution>
    <execution>
      <id>multi-thread-test</id>
      <phase>test</phase>
      <goals>
        <goal>test</goal>
      </goals>
      <configuration>
        <excludes>
          <exclude>**/SingleThreadedTest.java</exclude>
        </excludes>
        <forkCount>4</forkCount>
        <reuseForks>true</reuseForks>
        <parallel>all</parallel>
        <useUnlimitedThreads>true</useUnlimitedThreads>
        <skip>false</skip>
      </configuration>
    </execution>
  </executions>
</plugin>
相关问题