You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将终端Cucumber标签过滤器与JUnit Runner标签拼接

实现Cucumber命令行标签与Runner默认标签的自动拼接

现状与需求

当前JUnit Runner的Cucumber配置如下:

@CucumberOptions(plugin = {"pretty", "json:target/cucumber_parallel.json", "junit:target/cucumber_parallel-junit-report.xml",
        "rerun:target/failed_scenarios/failed_parallel_once.txt", "timeline:target/timeline"},
        features = {"classpath:features/parallel"},
        tags = "(not @Ignored)",
        glue = {"com.intrasoft.swp.trader.stepdefs"},
        monochrome = true)

执行测试时使用Maven命令:

mvn clean test -Dcucumber.filter.tags="@one"

问题在于命令行传入的cucumber.filter.tags会覆盖@CucumberOptions中的tags配置,导致无法自动排除@Ignored标签。需要实现命令行标签与默认标签的自动拼接,最终生效的过滤器为@one and (not @Ignored),避免手动添加遗漏。

解决方案一:自定义Cucumber属性处理器

通过自定义属性处理器来合并命令行与默认标签:

  • 创建自定义属性处理类:
import io.cucumber.core.options.Constants;
import io.cucumber.core.options.PropertiesParser;
import java.util.Properties;

public class CombinedTagProvider implements Constants {

    public static String getTags() {
        Properties props = System.getProperties();
        PropertiesParser parser = new PropertiesParser(props);
        
        String defaultTags = "(not @Ignored)";
        String cliTags = parser.get(CUCUMBER_FILTER_TAGS_PROPERTY).orElse(null);
        
        if (cliTags != null && !cliTags.isBlank()) {
            return cliTags + " and " + defaultTags;
        }
        return defaultTags;
    }
}
  • 修改Runner类,引用该类的方法:
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty", "json:target/cucumber_parallel.json", "junit:target/cucumber_parallel-junit-report.xml",
        "rerun:target/failed_scenarios/failed_parallel_once.txt", "timeline:target/timeline"},
        features = {"classpath:features/parallel"},
        tags = CombinedTagProvider.getTags(),
        glue = {"com.intrasoft.swp.trader.stepdefs"},
        monochrome = true)
public class ParallelTestRunner {
}

执行原Maven命令后,Cucumber会自动使用拼接后的标签过滤器@one and (not @Ignored)。

解决方案二:Maven属性拼接

无需修改Java代码,通过Maven Surefire插件配置实现标签合并:

  • 在pom.xml中添加属性配置:
<properties>
    <cucumber.default.tags>(not @Ignored)</cucumber.default.tags>
</properties>
  • 配置Surefire插件:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M7</version>
            <configuration>
                <systemPropertyVariables>
                    <cucumber.filter.tags>
                        ${if(${cucumber.filter.tags}, ${cucumber.filter.tags} and ${cucumber.default.tags}, ${cucumber.default.tags})}
                    </cucumber.filter.tags>
                </systemPropertyVariables>
            </configuration>
        </plugin>
    </plugins>
</build>

执行测试时,直接使用原命令:

mvn clean test -Dcucumber.filter.tags="@one"

Maven会自动拼接标签,未传入命令行标签时则仅使用默认的(not @Ignored)。

内容的提问来源于stack exchange,提问作者Luca Ruggeri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 22:51:22