Cucumber-JUnit 使用标签控制并行测试

发布于 2025-01-14 14:13:13 字数 1464 浏览 6 评论 0原文

最近,我使用 Cucumber-JUnit 创建了一个框架,我可以在其中并行执行场景(目前每个功能保留一个场景),没有任何问题。

现在我遇到的情况是某些功能必须并行运行,有些功能必须按顺序运行

我们是否可以通过标签或任何其他配置进行控制来选择哪些必须并行或顺序运行?

让我概述一下它目前是如何

并行的,以及根据 Cucumber 官方文档控制其线程大小 - Maven Surefire

pom.xml

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>3.0.0-M5</version>
   <configuration>
       <parallel>methods</parallel>
       <threadCount>${threadSize}</threadCount>
       <perCoreThreadCount>false</perCoreThreadCount>
       </configuration>
</plugin>
        
        
        

CucumberRunner:

@RunWith(Cucumber.class)
@CucumberOptions(
        features = {"src/test/resources/features"},
        glue = {"com.tests.binding.steps"},
        tags = "@regression"
)
public class RunCucumberFeatures {

}

命令用于运行测试

mvn clean test -Dcucumber.filter.tags="${toExecute} and not (@smoke)" -DthreadCount=${ThreadSize} -Dcucumber.execution.dry-run="false"

>toExecute 参数 - 我们传递多个标签,例如 @customerClaim 或 @employeeClaim

现在,在我的例子中,具有标签 @employeeClaim应并行执行,带有 @customerClaim 的标签应按顺序执行

目前的设计或其他方式是否可行?

Recently I created a framework with Cucumber-JUnit where I am able to execute Scenarios in parallel (For now keeping one scenario per feature) without any issue.

Now I have a situation where some of the features has to run in parallel and some in sequence.

Is there any way that we can control with tags or any other configuration to choose which has to run in parallel or sequence?

Let me put some overview how it is currently

Parallel and its thread size controlled as per cucumber official documentation - Maven surefire

pom.xml

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>3.0.0-M5</version>
   <configuration>
       <parallel>methods</parallel>
       <threadCount>${threadSize}</threadCount>
       <perCoreThreadCount>false</perCoreThreadCount>
       </configuration>
</plugin>
        
        
        

CucumberRunner:

@RunWith(Cucumber.class)
@CucumberOptions(
        features = {"src/test/resources/features"},
        glue = {"com.tests.binding.steps"},
        tags = "@regression"
)
public class RunCucumberFeatures {

}

command using to run tests

mvn clean test -Dcucumber.filter.tags="${toExecute} and not (@smoke)" -DthreadCount=${ThreadSize} -Dcucumber.execution.dry-run="false"

For toExecute parameter - we pass multiple tags like @customerClaim or @employeeClaim

Now, In my case features with tags @employeeClaim should execute in parallel and tags with @customerClaim should execute in sequence.

Is it possible with current design or any other way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最偏执的依靠 2025-01-21 14:13:13

有什么方法可以让我们通过标签或任何其他配置进行控制来选择哪个必须并行运行还是顺序运行?

cucumber-junit 和 JUnit 4 则不然。但是,使用 JUnit 5,您可以使用 cucumber-junit-platform-engine 并使用 JUnit 5s 对独占资源的支持。

https://github.com/cucumber/cucumber-jvm/树/主/junit-platform-engine

要同步特定资源上的方案,必须对该方案进行标记,并将该标记映射到特定资源的锁。资源由任意字符串标识,可以使用读写锁或读锁进行锁定。

例如以下标签:

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=java.lang.System.properties
cucumber.execution.exclusive-resources.reads-system-properties.read=java.lang.System.properties

当执行第一个带有@reads-and-writes-system-properties标签的场景时,会使用读写锁锁定java.lang.System.properties资源,并且不会与锁定java.lang.System.properties资源的第二个场景同时执行具有读锁的相同资源。

注意:标签中的@不包含在属性名称中。注意:有关规范资源名称,请参阅 junit5/Resources.java

因此,通过为 @customerClaim 创建独占资源,您可以防止这些场景并行运行。然而,据我所知,JUnit 5 不保证执行顺序,因此它们仍然应该彼此独立。

Is there any way that we can control with tags or any other configuration to choose which has to run in parallel or sequence?

Not with cucumber-junit and JUnit 4. However with JUnit 5 you can use the cucumber-junit-platform-engine and use JUnit 5s support for exclusive resources.

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

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 an arbitrary 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=java.lang.System.properties
cucumber.execution.exclusive-resources.reads-system-properties.read=java.lang.System.properties

when executing the first scenario tagged with @reads-and-writes-system-properties will lock the java.lang.System.properties resource with a read-write lock and will not be concurrently executed with the second scenario that locks the same resource with a read lock.

Note: The @ from the tag is not included in the property name. Note: For canonical resource names see junit5/Resources.java

So by making an exclusive resource for @customerClaim you can prevent these scenarios from running in parallel. However to the best of my knowledge JUnit 5 makes no guarantees made about the order of execution so they should still be independent from each other.

清引 2025-01-21 14:13:13

如果我们可以仅通过标签选择并行运行哪些测试,那就太好了。我很快就会与微服务作斗争,我希望我的大部分测试能够并行运行以节省时间。到目前为止,我一直在测试一个整体,并且我的框架已经完成了多个 .feature 文件和配置为在不同环境上运行的标签。因此,如果有一种简单的方法来配置哪些测试并行运行,其余的测试使用标签按顺序运行——那么很高兴知道。

It would be nice if we could select which tests to run in parallel only by tags. I will be fighting with microservices very soon and I would like most of my tests to run in parallel to save time. So far I have been testing a monolith and my framework is already done with multiple .feature files and tags configured to run on different environments. So if there is an easy way to configure which tests to run in parallel and the rest in sequence using tags - it would be nice to know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文