Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surefire run certain tests in sequence, others in parallel

Is there a way to make certain tests run in in sequence with the surefire plugin for JUnit? Currently it is running all tests in parallel, but some tests are very hard to convert and it would be sufficient if they didn't run in parallel. Here is a part of our pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <dependencies>
      <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-junit47</artifactId>
        <version>2.17</version>
      </dependency>
    </dependencies>
    <configuration>
        <parallel>all</parallel>
        <threadCount>8</threadCount>
      <includes>
        <include>${includeTest}</include>
        <include>${includeAdditionalTest}</include>
      </includes>
    </configuration>
</plugin>
like image 214
Mattias Avatar asked Sep 10 '25 10:09

Mattias


2 Answers

Consult the surefire plugin documentation. It provides a way to specify that certain tests are not thread safe by using @NotThreadSafe annotation.

Another solution is to specify two separate surefire executions with explicit test inclusions and exclusions. One execution could run in parallel mode including the thread safe suites, the other those non thread safe.

like image 146
Piotrek Bzdyl Avatar answered Sep 12 '25 23:09

Piotrek Bzdyl


We can achieve by defining multiple executions

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <executions>
        <execution>
            <id>unit-test</id>
            <phase>unit-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <parallel>methods</parallel>
                <threadCount>4</threadCount>
                <perCoreThreadCount>true</perCoreThreadCount>
                <excludes>
                    <!-- run all UTs (non-ITs) parallel -->
                    <exclude>**/*IT.java</exclude>
                </excludes>
            </configuration>
        </execution>
        <execution>
            <id>integration-test</id>
            <phase>integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>                 
                <excludes>
                    <exclude>none</exclude>
                </excludes>
                <includes>
                    <!-- run integration tests sequentially -->
                    <include>**/*IT.java</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>
like image 21
Sridivakar Avatar answered Sep 13 '25 01:09

Sridivakar