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

如何在Java中从YML文件传递Cucumber标签名

从外部YAML文件传递Cucumber标签到TestRunner的解决方案

由于新版Cucumber(v7+)重构了内部API,旧的Runtime、MultiLoader等类已被移除,因此需要采用适配新版的方案,以下提供两种可行方式:

方式一:通过Java代码读取YAML并设置系统属性

这种方式无需依赖Maven插件,直接在代码中读取YAML文件并传递标签。

  1. 添加SnakeYAML依赖
    在pom.xml中加入读取YAML的依赖:
<dependency>
    <groupId>org.yaml</groupId>
    <artifactId>snakeyaml</artifactId>
    <version>2.2</version>
    <scope>test</scope>
</dependency>
  1. 编写YAML读取工具类
    创建工具类读取run.yml中的标签值:
import org.yaml.snakeyaml.Yaml;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

public class YamlReader {
    public static String getCucumberTag() {
        Yaml yaml = new Yaml();
        // 根据实际文件路径调整,若放在src/test/resources下则改为"src/test/resources/run.yml"
        try (FileInputStream fis = new FileInputStream("run.yml")) {
            Map<String, Object> yamlData = yaml.load(fis);
            Map<String, String> execution = (Map<String, String>) yamlData.get("execution");
            return execution.get("tag");
        } catch (IOException e) {
            e.printStackTrace();
            return "@Regression"; // 读取失败时使用默认标签
        }
    }
}
  1. 修改TestRunner类
    通过静态代码块设置系统属性,Cucumber会优先读取该属性值作为标签:
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "features",
        glue = "io/automation/gluecode",
        plugin = {"json:target/cucumber.json"}
)
public class TestRunner {
    static {
        String tag = YamlReader.getCucumberTag();
        // 新版Cucumber使用cucumber.filter.tags作为标签系统属性
        System.setProperty("cucumber.filter.tags", tag);
    }
}

方式二:通过Maven插件读取YAML并传递参数

这种方式利用Maven生态处理YAML,无需编写额外Java工具类。

  1. 添加Maven YAML读取插件
    在pom.xml中加入读取YAML的插件,将YAML中的标签转为Maven属性:
<plugin>
    <groupId>com.github.herve-thouzard</groupId>
    <artifactId>maven-yaml-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <execution>
            <goals>
                <goal>read</goal>
            </goals>
            <configuration>
                <files>
                    <file>run.yml</file>
                </files>
                <prefix>yaml</prefix>
            </configuration>
        </execution>
    </executions>
</plugin>
  1. 配置Surefire插件传递参数
    修改Surefire插件,将读取到的YAML标签设置为系统属性:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.2.5</version>
    <configuration>
        <systemPropertyVariables>
            <cucumber.filter.tags>${yaml.execution.tag}</cucumber.filter.tags>
        </systemPropertyVariables>
    </configuration>
</plugin>
  1. 调整TestRunner类
    移除硬编码的tags配置,或保留默认值作为 fallback:
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
        tags = "${cucumber.filter.tags:@Regression}", // 系统属性不存在时使用默认值
        features = "features",
        glue = "io/automation/gluecode",
        plugin = {"json:target/cucumber.json"}
)
public class TestRunner {
}

关键说明

  • 新版Cucumber使用cucumber.filter.tags作为标签的系统属性名,替代旧版的cucumber.options。
  • 若直接通过JUnit执行测试,方式一的静态代码块会自动读取YAML;若通过Maven执行,两种方式均生效。

内容的提问来源于stack exchange,提问作者Fayis K

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 16:52:04