Exclude specific tests from being run in parallel

2019-04-06 11:07发布

问题:

I recently stumbled upon a simple way to parallelize the execute of tests via jUnit by specifying the following in a java project's pom.xml file:

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

I've discovered that there are 2 test-classes (let's call them "badtestclass1" and "badtestclass2") that keep getting penalized by this parallel execution due to the way the tests in them have been written. Ideally, I would refactor those test-classes to behave better, but in the interim, I was wondering if there's a nifty way to "exclude" these specific classes from being executed in parallel. Basically, is there a way to execute everything else in parallel and then these 2 in sequence (or the other order, doesn't matter). Would something like the following work?

<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>

回答1:

Exclude those 2 tests in the original test phrase and then create a new execution with those 2 classes running in single thread? :)

<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:

You can annotate the classes you don't want parallelized with jcip @NotThreadSafe and leave the surefire configuration as it was in your starting example. This way, whenever surefire finds an annotated class it just executes it in a single thread. It's explained right here in the "Parallel Test Execution and Single Thread Execution" paragraph.