gpt4 book ai didi

maven - 有什么方法可以并行运行 JUnit5 测试?

转载 作者:行者123 更新时间:2023-12-01 04:38:22 24 4
gpt4 key购买 nike

以前我使用 Maven+Selenide+JUnit4 进行我的测试,它很好,并行运行效果很好。例子:

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin}</version>
<configuration>
<parallel>all</parallel>
<perCoreThreadCount>true</perCoreThreadCount>
<threadCount>4</threadCount>
<perCoreThreadCount>false</perCoreThreadCount>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>

在 Jenkins 的工作中,我能够运行测试(下面的示例)
mvn -Dtest=TestClassName test

我的测试在 4 个浏览器中运行。

在我切换到JUnit5之前,因为我想使用标签运行测试,例如
@Test
@Tag("smoke")
public void test1() {}

并通过下一个命令运行所有标记为“smoke”的测试:
mvn -Dtag=smoke test

但是我遇到了下一个问题:并行执行不起作用,我仍然没有找到解决方案。
我发现了这个错误 https://github.com/junit-team/junit5/issues/1424

如何与 JUnit5 并行运行测试?

我曾尝试在 中使用pom.xml
<forkCount>2</forkCount>
<reuseForks>true</reuseForks>
<parallel>all</parallel>

它没有帮助,我创建了一个文件 junit-platform.properties 并插入那里
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.config.strategy = fixed

但无论如何我无法解决这个问题。

最佳答案

最后我找到了解决方案。

在 Maven+JUnit5 上,并行执行只能通过类工作(而不是通过我在 JUnit4 中习惯的方法)

如何实现:
只需将这 2 个字符串放入您的 pom.xml :

<forkCount>4</forkCount>
<reuseForks>false</reuseForks>

例子:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<forkCount>4</forkCount>
<reuseForks>false</reuseForks>
<properties>
<includeTags>${tag}</includeTags>
</properties>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-logger-api</artifactId>
<version>${surefire-logger-api}</version>
</dependency>
</dependencies>
</plugin>

例如,您有 3 个带有测试的类,因此在从控制台运行当前测试后,将创建浏览器的 3 个实例(每个类一个),并且在每个类中测试将一致地执行,但类是并行执行的。

关于maven - 有什么方法可以并行运行 JUnit5 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51308145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com