运行 JUnit5 并行测试的任何方法?

Any way to run JUnit5 tests in parallel?

之前我使用 Maven+Selenide+JUnit4 进行测试,结果很好,并行 运行ning 工作得很好。示例:

<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 之前,因为我想通过标签使用 运行ning 测试,例如

@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 上,并行执行只能通过 classes 进行(而不是通过我在 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 个 class 测试,因此在 运行 从控制台当前测试后,将创建 3 个浏览器实例(每个 class 一个)和内部每个 class 测试将一致执行,但 classes 是并行执行的。