问题描述
最近得到了一份新工作,我正在从事 Maven 和 Micronaut 项目 - 我是 Micronaut 的新手,如果您不熟悉,它是 Spring 的替代品。
以此为指导:https://www.baeldung.com/maven-junit-parallel-tests
我正在尝试让我们的集成测试并行运行,以加快我们的测试速度。我已将所需的配置添加到 pom.xml 文件中。我已经调整了很多,使用不同的设置组合(包括我在其他 SO 帖子中找到的设置)。
但是当我运行测试时,无论是否添加了这个添加的配置,运行它们所需的时间是完全相同的 - 1 分 38 秒。所以我认为它不起作用。虽然不知道我在控制台上的预期输出应该是什么样子,如果它正常工作?
在测试中,他们都没有使用@RunWith,它使用默认值,应该没问题。
这是pom。任何人都可以请指教吗?谢谢。
<profiles>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludedGroups>unit,performance</excludedGroups>
<parallel>classes</parallel>
<threadCount>10</threadCount>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>performance</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludedGroups>unit,integration</excludedGroups>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>all</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludedGroups>none</excludedGroups>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<excludedGroups>integration,performance</excludedGroups>
<parallel>classes</parallel>
<threadCount>10</threadCount>
</configuration>
</plugin>
...
</plugins>
</build>
解决方法
我遇到了同样的问题并尝试了以下所有组合:
-
maven-surefire-plugin(2.22.0 版)
-
maven-failsafe-plugin(2.22.0 版)
- 这是另一个基本相同的插件,但设计用于运行集成测试,因为它不会在第一次失败的测试后停止。
-
你提到的 parallel/threadCount 配置。
-
这些不同的插件配置在 https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven-config-params 和 https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution 处指定。就像这样:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.22.0</version> <configuration> <properties> <configurationParameters> junit.jupiter.execution.parallel.enabled=true junit.jupiter.execution.parallel.mode.default=same_thread junit.jupiter.execution.parallel.mode.classes.default=concurrent </configurationParameters> </properties> </configuration> </plugin>
唯一有效的组合是 2 (maven-failsafe-plugin) 和 4 (configurationParameters)。
,您可以尝试使用配置选项 forkCount
吗?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
...
<forkCount>4</forkCount>
...
</configuration>
</plugin>
来自官方xml元素描述
用于指定要并行分叉以执行测试的虚拟机数量的选项。当以“C”结尾时, 数字部分乘以 CPU 内核数。浮点值仅与 “C”。如果设置为“0”,则不会派生 VM,所有测试都在主进程内执行。
示例值:“1.5C”、“4”