在自动化测试体系中,Cucumber凭借行为驱动开发的特性被广泛使用,而Maven作为常用的项目管理工具,能够很好地配合Cucumber实现测试流程的灵活管控。通过合理的标签过滤和并行执行配置,可以大幅降低测试执行的时间成本,提升测试反馈效率。

一、Cucumber标签的基础使用
Cucumber的标签是附加在Feature文件场景或整个Feature上的标记,用于分类管理测试用例。我们可以在场景定义时添加标签,例如:
# 登录功能相关场景
@login @smoke
Feature: 用户登录功能测试
@valid_credentials
Scenario: 使用正确的账号密码登录
Given 打开登录页面
When 输入正确的用户名"test"和密码"123456"
Then 登录成功并跳转到首页
@invalid_credentials
Scenario: 使用错误的账号密码登录
Given 打开登录页面
When 输入错误的用户名"test"和密码"wrong"
Then 页面提示账号或密码错误
上述代码中,@login、@smoke、@valid_credentials都是标签,我们可以通过这些标签筛选需要执行的场景。
二、Maven中配置Cucumber高级标签过滤
在Maven项目中,我们通常在pom.xml的Cucumber插件配置中设置标签过滤规则,支持多标签组合、排除标签等高级用法。
1. 基础标签过滤配置
首先在pom.xml中添加Cucumber相关依赖:
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
</dependencies>
然后在Maven Surefire插件中配置Cucumber的标签过滤参数:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<test>io.cucumber.junit.Cucumber</test>
<properties>
<property>
<name>cucumber.features</name>
<value>src/test/resources/features</value>
</property>
<!-- 只执行带有@smoke标签的场景 -->
<property>
<name>cucumber.filter.tags</name>
<value>@smoke</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
</build>
2. 高级标签组合过滤
Cucumber支持标签的逻辑组合,常见的组合规则如下:
@tag1 and @tag2:同时包含两个标签的场景才执行@tag1 or @tag2:包含任意一个标签的场景就执行not @tag1:排除带有该标签的场景- 组合使用:
(@smoke or @regression) and not @skip,表示执行带有smoke或regression标签,同时排除skip标签的场景
对应的Maven配置只需要修改cucumber.filter.tags的值即可:
<property>
<name>cucumber.filter.tags</name>
<value>(@smoke or @regression) and not @skip</value>
</property>
三、Maven中配置Cucumber并行执行策略
Cucumber从7.x版本开始原生支持并行执行,我们可以通过Maven配置开启并行能力,充分利用多核CPU资源。
1. 基础并行执行配置
在Surefire插件中添加并行执行参数,设置并行执行线程数:
<configuration>
<test>io.cucumber.junit.Cucumber</test>
<properties>
<property>
<name>cucumber.features</name>
<value>src/test/resources/features</value>
</property>
<property>
<name>cucumber.filter.tags</name>
<value>@regression</value>
</property>
<!-- 开启并行执行,设置线程数为4 -->
<property>
<name>cucumber.execution.parallel.enabled</name>
<value>true</value>
</property>
<property>
<name>cucumber.execution.parallel.config.fixed.parallelism</name>
<value>4</value>
</property>
</properties>
</configuration>
2. 基于Feature文件的并行策略
默认情况下Cucumber会按场景级别并行,我们也可以调整为按Feature文件级别并行,适合单个Feature内场景较少的场景:
<property>
<name>cucumber.execution.parallel.config.strategy</name>
<value>feature</value>
</property>
3. 动态线程数配置
我们还可以根据CPU核心数动态设置线程数,避免资源过度占用:
<property>
<name>cucumber.execution.parallel.config.fixed.parallelism</name>
<value>${available_cores}</value>
</property>
对应的Maven属性配置:
<properties>
<available_cores>${Runtime.getRuntime().availableProcessors()}</available_cores>
</properties>
四、标签过滤与并行执行结合使用
实际项目中我们通常会同时开启标签过滤和并行执行,只运行需要测试的场景,同时提升执行效率:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<test>io.cucumber.junit.Cucumber</test>
<properties>
<property>
<name>cucumber.features</name>
<value>src/test/resources/features</value>
</property>
<!-- 高级标签过滤:执行smoke或login相关,排除skip标签 -->
<property>
<name>cucumber.filter.tags</name>
<value>(@smoke or @login) and not @skip</value>
</property>
<!-- 开启并行执行 -->
<property>
<name>cucumber.execution.parallel.enabled</name>
<value>true</value>
</property>
<!-- 设置并行线程数为CPU核心数 -->
<property>
<name>cucumber.execution.parallel.config.fixed.parallelism</name>
<value>${runtime.available_processors}</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<runtime.available_processors>${Runtime.getRuntime().availableProcessors()}</runtime.available_processors>
<cucumber.version>7.15.0</cucumber.version>
</properties>
五、注意事项
- 并行执行时需要确保测试场景之间没有共享状态,避免出现测试结果互相影响的问题
- 标签过滤的表达式需要符合Cucumber的标签语法规则,错误表达式会导致无用例执行
- 如果使用的是JUnit5,需要对应调整Cucumber的测试引擎依赖,配置方式略有差异
- 并行线程数不是越多越好,需要根据测试环境和用例特性合理设置,避免资源竞争导致执行效率下降
通过上述配置,我们可以灵活控制Cucumber测试用例的执行范围,同时提升执行效率,让自动化测试更好地融入持续集成流程,快速反馈代码质量问题。