【问题标题】:Any way to run JUnit5 tests in parallel?有什么方法可以并行运行 JUnit5 测试?
【发布时间】:2018-07-12 14:24:00
【问题描述】:

以前我使用 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() {}

并运行下一条命令标记为“冒烟”的所有测试:

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 parallel-processing junit5 selenide


【解决方案1】:

终于找到了解决办法。

在 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。
  • 嗨 Gordon Freeman,请问如何在 gralde 项目中添加 fork 设置以及添加它有什么用。 4false 我在 junit-platform.properties 文件中使用以下配置,在两个浏览器瞬间,但我仍然看到一些问题 junit.jupiter.execution.parallel.enabled = true junit.jupiter.execution.parallel.mode.default = same_thread junit.jupiter.execution.parallel.mode.classes.default = 并发 junit.jupiter.execution.parallel.config.strategy = 固定 junit.jupiter.execution.parallel。 config.fixed.parallelism=2
猜你喜欢
  • 2020-09-10
  • 2018-06-07
  • 2019-05-18
  • 1970-01-01
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多