问题
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>
回答1:
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.
回答2:
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>
回答3:
Yes,
Put each sequential test in its own suite and all parallel tests together in a suite. Then set parallel attribite to classes.
<configuration>
<includes>
<include>**/A01TestSuite.java</include>
<include>**/A02ServiceTestSuite.java</include>
<include>**/A03FlowTestSuite.java</include>
</includes>
<additionalClasspathElements>
<additionalClasspathElement>${webinf.dir
</additionalClasspathElement>
</additionalClasspathElements>
<systemPropertyVariables>
<log4j.configuration>file:${l4j.test}/log4j.test.properties</log4j.configuration>
</systemPropertyVariables>
<forkMode>always</forkMode>
<argLine>-Xms512m -Xmx512m</argLine>
<parallel>classes</parallel>
<threadCount>10</threadCount>
</configuration>
来源:https://stackoverflow.com/questions/29984775/surefire-run-certain-tests-in-sequence-others-in-parallel