有没有办法在通过surefire插件执行时排除特征文件包?

问题描述

所以我必须并行执行cucumber文件,但有些情况不能并行运行,所以我需要根据包或文件排除它们。

我知道 .java 文件支持功能,但我们对 .feature 文件有类似的支持吗?

在 JUnit 和 Surefire 插件中使用 Cucumber

这是一个片段

    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                    <configuration>
                        <parallel>all</parallel>
                        <threadCount>3</threadCount>
                        <excludes>
                            <exclude>You can exclude java files from here but not .feature files</exclude>
                        </excludes>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjweaver</artifactId>
                            <version>${aspectj.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>

解决方法

您可以使用可以在 maven-surefire-plugin 配置对象中作为系统属性提供的标签来排除功能文件。

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <parallel>all</parallel>
                    <threadCount>3</threadCount>
                    <!-- Ignore tag System Property below -->
                    <systemPropertyVariables>
                         <cucumber.filter.tags>not @ignore</cucumber.filter.tags>
                    </systemPropertyVariables>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

使用 junit-platform-engine 也允许使用 maven excludeGroups (https://github.com/cucumber/cucumber-jvm/tree/main/junit-platform-engine#configuration-options)

,

当与 Cucumber 和 JUnit 5 并行执行场景时,而不是将应该串行运行的场景放在单独的测试执行中,您还可以在特定资源上进行同步。

执行此操作时,Junit 平台将以这样一种方式安排测试,即不应该同时运行的测试不会这样做。

https://github.com/cucumber/cucumber-jvm/tree/main/junit-platform-engine#exclusive-resources

要在特定资源上同步场景,场景必须是 标记并且此标记映射到特定资源的锁。一种 资源由字符串标识,可以用 读写锁,或读锁。

例如以下标签:

Feature: Exclusive resources

 @reads-and-writes-system-properties
 Scenario: first example
   Given this reads and writes system properties
   When it is executed 
   Then it will not be executed concurrently with the second example

 @reads-system-properties
 Scenario: second example
   Given this reads system properties
   When it is executed
   Then it will not be executed concurrently with the first example

使用此配置:

cucumber.execution.exclusive-resources.reads-and-writes-system-properties.read-write=SYSTEM_PROPERTIES 黄瓜.execution.exclusive-resources.reads-system-properties.read=SYSTEM_PROPERTIES

第一个场景标记为@reads-and-writes-system-properties 将使用读写锁锁定 SYSTEM_PROPERTIES 并且不会 与使用读锁的第二种场景并发执行。

作为资源值,您可以定义自己的资源,

或者使用众所周知的资源,例如:

https://github.com/junit-team/junit5/blob/main/junit-jupiter-api/src/main/java/org/junit/jupiter/api/parallel/Resources.java

为了让场景独立运行,您可以使用 org.junit.platform.engine.support.hierarchical.ExclusiveResource.GLOBAL_KEY

https://github.com/junit-team/junit5/blob/ae2a336fe4b371d398da386e8b336cc06329da7d/junit-platform-engine/src/main/java/org/junit/platform/engine/support/hierarchical/ExclusiveResource.java#L47