运行 cucumber 使用 cucumber-jvm 4 并行测试

run cucumber tests parallel using cucumber-jvm 4

Java v8.x - spring v5.x 黄瓜 v4.2.0

我尝试了 temyers/cucumber-jvm-parallel-plugin 并且工作正常,但是当我进入他们的 gitihub 页面时,他们宣布停止使用这个插件 b/c cucumber 已经开始支持并行测试 运行 支持来自 cucumber-jvm 4.0.0.

我有使用以下 Maven 依赖项的现有测试。

cucumber-java v4.2.0
cucumber-junit v4.2.0
cucumber-spring v4.2.0

我有两个问题让我很困惑。

  1. 为了使用 cucumber-jvm,我是否必须更改我的依赖关系,否则我当前的依赖关系将起作用。
  2. 我可以通过参数 --parallel 开始并行黄瓜测试吗?

感谢任何帮助。

cucumber-jvm:4.0.0 中引入了并行支持。所以你的依赖关系没问题。

如果您使用的是 Maven:

Cucumber JUnit - Parallel Execution with Maven

Cucumber JUnit supports parallel execution of feature files across multiple threads. To enable this with maven set the parallel property to either methods or both.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <parallel>both</parallel>
                <threadCount>4</threadCount>
            </configuration>
        </plugin>
    </plugins>
</build>

如果您使用 Gralde,最好使用 Gradle Cucumber 插件,因为 Gradle 不支持子 class 级并行化。在这种情况下,您不需要 cucumber-junit.

Gradle Cucumber runner - Running features in parallel

Cucumber supports parallel execution if you specify the number of thread to use. This can be done in two ways

  • An option whn running from a command line, ./gradlew cucumber --threads 4, this will run four parallel threads
  • An extension, that is setting the value in your build.gradle file in the cucumber section.

When setting the value in the build script, you can't really know how many threads the build will have access to. Hardcoding a fixed number may not be your best option.

One way of specifying the number of threads to use is:

cucumber {
    threads = = Runtime.runtime.availableProcessors().intdiv(2) ?: 1 
}

This will use as many threads as possible while leaving resources for Gradle to execute.