Is there a way you can exclude packages of feature file while doing execution via surefire plugin?

Viewed 463

So I have to make a parallel execution of cucumber files but there are some cases that can't be run in parallel so I need to exclude them on the basis of the package or the file.

I know this is supported for .java files but do we have something similar for .feature files ?

Using the cucumber with JUnit and Surefire plugin

Here is a snipet

    <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>
2 Answers

You can exclude feature files using tags that can be supplied in the maven-surefire-plugin configuration object as a system property.

<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>

Using junit-platform-engine allows to use maven excludedGroups too (https://github.com/cucumber/cucumber-jvm/tree/main/junit-platform-engine#configuration-options)

When executing scenarios in parallel with Cucumber and JUnit 5, rather putting the scenarios that should run serially in a separate test execution, you can also synchronize on specific resources.

When doing this the Junit Platform will schedule tests in such a way that tests are not supposed to run concurrently will not do so.

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

To synchronize a scenario on a specific resource, the scenario must be tagged and this tag mapped to a lock for the specific resource. A resource is identified by a String and can be either locked with a read-write-lock, or a read-lock.

For example, the following tags:

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

With this configuration:

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

The first scenario tagged with @reads-and-writes-system-properties will lock the SYSTEM_PROPERTIES with a read-write lock and will not be concurrently executed with the second scenario that uses a read lock.

As the resource value you can define your own resources,

Or use the well known resources such as:

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

And to make a scenario run in isolation you can use 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

Related