我正在开发 Maven 项目,其中包含数千个运行速度非常慢的测试(需要 2 小时才能运行所有测试)。因此,我尝试通过配置 Surefire 插件来并行运行测试,如下所示:
<configuration>
<failIfNoTests>false</failIfNoTests>
<reuseForks>false</reuseForks>
<reuseForks>true</reuseForks>
<forkCount>2C</forkCount>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
<parallel>suites</parallel>
<threadCount>12</threadCount>
</configuration>
但是一些测试在我们初始化和清理一些资源的 @Before
和 @After
方法中失败了(这似乎与端口冲突有关) 。
我尝试将此注释 @net.jcip.annotations.NotThreadSafe
添加到失败的测试中,如 surefire documentation 中所述。按顺序运行它们并避免冲突。然而这不起作用,这些测试仍然失败!!!
关于如何强制某些可靠的测试在同一 JVM 进程和同一线程上顺序运行,而其余测试将并行运行(可能在不同的 JVM 进程上)的任何指示?
编辑 1 现在,我尝试将 Surefire 的配置分为两种:一种用于顺序测试,另一种用于并行测试。然而,这种方法似乎并没有提高执行时间,因为我仍然有失败的测试,并且仍然需要 2 小时才能运行所有测试。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<!-- Sequential tests -->
<configuration>
<includes>**/*Sequential.java</includes>
<failIfNoTests>false</failIfNoTests>
<reuseForks>false</reuseForks>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
<executions>
<!-- Parallel tests -->
<execution>
<id>parallel-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>**/*.java</includes>
<excludes>
<exclude>**/*Sequential.java</exclude>
</excludes>
<failIfNoTests>false</failIfNoTests>
<reuseForks>true</reuseForks>
<forkCount>2C</forkCount>
<parallel>suites</parallel>
<threadCount>12</threadCount>
</configuration>
</execution>
</executions>
</plugin>
最佳答案
我会采用你的“将 Surefire 的配置分成两部分”的方法。
但是您的配置似乎有问题,因为您只定义了一个执行。
可能想尝试这样的事情(用自己的配置定义2个执行):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<executions>
<!-- Sequential tests -->
<execution>
<id>sequential-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>**/*Sequential.java</includes>
<failIfNoTests>false</failIfNoTests>
<reuseForks>false</reuseForks>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</execution>
<!-- Parallel tests -->
<execution>
<id>parallel-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>**/*.java</includes>
<excludes>
<exclude>**/*Sequential.java</exclude>
</excludes>
<failIfNoTests>false</failIfNoTests>
<reuseForks>true</reuseForks>
<forkCount>2C</forkCount>
<parallel>suites</parallel>
<threadCount>12</threadCount>
</configuration>
</execution>
</executions>
</plugin>
关于java - 如何并行运行万无一失的测试,同时强制某些测试按顺序运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42065544/