Cucumber JUnit5并行模式下场景执行顺序控制方案问询
答案
1. Cucumber JUnit5并行执行的默认场景分配顺序
Cucumber JUnit5的并行模式默认采用哈希负载均衡策略分配场景:
- 先收集所有符合条件的场景,再基于场景的哈希值将其均匀分配到线程池的各个线程中
- 完全不考虑场景在Feature文件中的顺序、标签类型或场景间的依赖关系
- 你看到
@validation场景被优先分配,只是哈希算法的随机结果(因为它们常是Feature的最后一个场景,哈希值刚好被分配到较早启动的线程),并非Cucumber刻意优先执行最后一个场景。
2. 控制场景执行顺序的可行方案
由于Cucumber并行模式本身不支持场景间的依赖调度,必须通过分阶段串行执行来满足依赖要求,用Maven Profiles可以实现一键执行所有阶段,具体方案如下:
核心思路
将测试分为三个串行阶段,每个阶段执行一类标签的场景,同一阶段内的场景可以并行执行(无依赖):
- 阶段1:执行所有
@capture场景(并行) - 阶段2:执行所有
@batchJob场景(可关闭并行,因数量少) - 阶段3:执行所有
@validation场景(并行)
pom.xml配置实现
在pom.xml中定义4个Profiles(3个单独阶段+1个组合阶段):
<profiles> <!-- 单独执行@capture场景 --> <profile> <id>run-capture</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> <configuration> <includes> <include>**/*Test.java</include> </includes> <systemPropertyVariables> <cucumber.execution.parallel.enabled>true</cucumber.execution.parallel.enabled> <cucumber.filter.tags>@capture</cucumber.filter.tags> </systemPropertyVariables> </configuration> </plugin> </plugins> </build> </profile> <!-- 单独执行@batchJob场景 --> <profile> <id>run-batchjob</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> <configuration> <includes> <include>**/*Test.java</include> </includes> <systemPropertyVariables> <cucumber.execution.parallel.enabled>false</cucumber.execution.parallel.enabled> <cucumber.filter.tags>@batchJob</cucumber.filter.tags> </systemPropertyVariables> </configuration> </plugin> </plugins> </build> </profile> <!-- 单独执行@validation场景 --> <profile> <id>run-validation</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> <configuration> <includes> <include>**/*Test.java</include> </includes> <systemPropertyVariables> <cucumber.execution.parallel.enabled>true</cucumber.execution.parallel.enabled> <cucumber.filter.tags>@validation</cucumber.filter.tags> </systemPropertyVariables> </configuration> </plugin> </plugins> </build> </profile> <!-- 一键执行所有阶段(串行) --> <profile> <id>run-all</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> <executions> <!-- 阶段1:执行@capture --> <execution> <id>run-capture-phase</id> <phase>test</phase> <goals> <goal>test</goal> </goals> <configuration> <systemPropertyVariables> <cucumber.execution.parallel.enabled>true</cucumber.execution.parallel.enabled> <cucumber.filter.tags>@capture</cucumber.filter.tags> </systemPropertyVariables> </configuration> </execution> <!-- 阶段2:执行@batchJob --> <execution> <id>run-batchjob-phase</id> <phase>test</phase> <goals> <goal>test</goal> </goals> <configuration> <systemPropertyVariables> <cucumber.execution.parallel.enabled>false</cucumber.execution.parallel.enabled> <cucumber.filter.tags>@batchJob</cucumber.filter.tags> </systemPropertyVariables> </configuration> </execution> <!-- 阶段3:执行@validation --> <execution> <id>run-validation-phase</id> <phase>test</phase> <goals> <goal>test</goal> </goals> <configuration> <systemPropertyVariables> <cucumber.execution.parallel.enabled>true</cucumber.execution.parallel.enabled> <cucumber.filter.tags>@validation</cucumber.filter.tags> </systemPropertyVariables> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>
执行方式
- 单独执行某阶段:
mvn clean install -P run-capture(替换为run-batchjob或run-validation) - 一键执行所有阶段:
mvn clean install -P run-all
之前antrun尝试失败的原因
大概率是antrun插件没有正确调用Surefire的多阶段执行逻辑,或者没有传递正确的Cucumber标签过滤参数。上述方案直接使用Surefire的多execution配置,确保三个阶段严格串行执行,每个阶段内的场景可以并行。
补充配置
如果需要指定并行线程数,可以在systemPropertyVariables中添加:
<cucumber.execution.parallel.config.strategy>fixed</cucumber.execution.parallel.config.strategy> <cucumber.execution.parallel.config.fixed.parallelism>4</cucumber.execution.parallel.config.fixed.parallelism>
内容的提问来源于stack exchange,提问作者Avi
相关产品推荐
相关产品推荐

