java SureFire按顺序运行某些测试,其他测试并行运行

eiee3dmh  于 2023-08-02  发布在  Java
关注(0)|答案(3)|浏览(161)

有没有一种方法可以让某些测试在JUnit的surefire插件中按顺序运行?目前,它并行运行所有测试,但有些测试很难转换,如果它们不并行运行就足够了。下面是我们的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>

字符串

e0uiprwp

e0uiprwp1#

请参阅surefire plugin documentation。它提供了一种通过使用@NotThreadSafe注解来指定某些测试不是线程安全的方法。
另一种解决方案是用显式的test inclusions and exclusions指定两个独立的万无一失的执行。一个执行可以在包括线程安全套件的并行模式下运行,另一个执行可以在非线程安全的模式下运行。

dfty9e19

dfty9e192#

我们可以通过定义多个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>

字符串

gojuced7

gojuced73#

是的,
将每个顺序测试放在它自己的套件中,并将所有并行测试放在一个套件中。然后设置类的平行属性。

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

字符串

相关问题