有没有办法并行运行JUnit5测试?

6

之前我使用 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

但是我遇到了另一个问题:并行执行不起作用,我仍然没有找到解决方法。 我发现这个bug: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

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


你正在使用哪些不同的JUnit JAR版本? - Marc Philipp
1
你有没有阅读用户指南中的说明?https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-parallel-execution - Sam Brannen
当然我已经阅读了。你之前检查过我所描述的内容吗?我无法并行运行。 - Gordon Freeman
你的 junit-platform.properties 文件位于哪里?你能否分享一个示例项目,演示测试不会并行运行? - Sam Brannen
当然,我的 junit-platform.properties 文件在 https://github.com/13Dima13/G/blob/master/junit-platform.properties。 - Gordon Freeman
显示剩余4条评论
1个回答

5

最终我找到了解决方法。

在 Maven+JUnit5 中,仅通过类(而不是像 JUnit4 一样通过方法)可以实现并行执行。

如何实现: 只需将这两个字符串放入您的 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-surefire-plugin 2.22.2 可以正常工作,但版本为 3.0.0-M3 的插件在我们的项目中与 Junit 5 不兼容。 - Kees van Dieren
嗨,Gordon Freeman,您能告诉我如何在gradle项目中添加fork设置以及添加它的用途吗?<forkCount>4</forkCount><reuseForks>false</reuseForks>我在junit-platform.properties文件中使用以下配置,在两个浏览器实例中,但仍然存在一些问题。junit.jupiter.execution.parallel.enabled = true junit.jupiter.execution.parallel.mode.default = same_thread junit.jupiter.execution.parallel.mode.classes.default = concurrent junit.jupiter.execution.parallel.config.strategy=fixed junit.jupiter.execution.parallel.config.fixed.parallelism=2 - sam
这对于并行运行RestAssured非常有帮助,因为RestAssured依赖于一个静态配置,不能在线程之间轻松共享。 - Harold L. Brown
这对于在并行中运行RestAssured很有帮助,因为RestAssured依赖于静态配置,这些配置不能在线程之间轻松共享。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接