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

QAF集成Cucumber-TestNG:用YAML替代Properties及报错解决

解决方案:Cucumber-TestNG集成QAF用YAML配置+CSV数据驱动

1. 添加适配依赖

确保pom.xml中包含QAF核心、Cucumber适配、YAML支持的依赖,同时规避版本冲突:

<dependencies>
    <!-- QAF核心依赖 -->
    <dependency>
        <groupId>com.qmetry</groupId>
        <artifactId>qaf</artifactId>
        <version>3.2.0</version>
    </dependency>
    <!-- QAF与Cucumber适配 -->
    <dependency>
        <groupId>com.qmetry</groupId>
        <artifactId>qaf-cucumber</artifactId>
        <version>3.2.0</version>
        <scope>test</scope>
    </dependency>
    <!-- QAF YAML配置支持 -->
    <dependency>
        <groupId>com.qmetry</groupId>
        <artifactId>qaf-provider-yaml</artifactId>
        <version>3.2.0</version>
        <scope>test</scope>
    </dependency>
    <!-- SnakeYAML(QAF YAML依赖) -->
    <dependency>
        <groupId>org.yaml</groupId>
        <artifactId>snakeyaml</artifactId>
        <version>1.33</version>
        <scope>test</scope>
    </dependency>
    <!-- TestNG -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.8.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

2. 配置TestNG XML指定YAML文件

QAF默认会查找application.properties,需在testng.xml中显式指定YAML配置路径,避免找不到配置的错误:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Cucumber-QAF-Suite" verbose="1">
    <parameter name="config.file" value="resources/config/test_config.yaml"/>
    <test name="Cucumber-QAF-Test">
        <classes>
            <class name="com.yourpackage.runner.CucumberQAFTestRunner"/>
        </classes>
    </test>
</suite>

3. 编写YAML配置文件

在指定路径创建YAML配置,替代Properties文件,可配置QAF基础参数:

# test_config.yaml
qaf:
  selenium:
    driver.name: chromeDriver
  resources:
    base.dir: resources

4. Feature文件中指定CSV数据源

将CSV文件放在resources/testdata目录下,通过Feature的Meta-data指定路径(QAF默认从classpath查找):

Feature: 用户登录测试
  @dataFile: testdata/user_login.csv
  Scenario Outline: 数据驱动登录验证
    Given 打开登录页面
    When 输入账号 <username> 和密码 <password>
    Then 验证登录结果为 <result>

    Examples:
      | username | password | result |

5. 调整Test Runner类

确保Runner类继承QAF适配的Cucumber基类,完成初始化:

package com.yourpackage.runner;

import com.qmetry.qaf.automation.step.client.gherkin.GherkinScenarioFactory;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.BeforeClass;

@CucumberOptions(
        features = "src/test/resources/features",
        glue = {"com.yourpackage.steps", "com.qmetry.qaf.automation.step.client.gherkin"},
        plugin = {"com.qmetry.qaf.automation.report.QAFReportPlugin"}
)
public class CucumberQAFTestRunner extends AbstractTestNGCucumberTests {
    @BeforeClass
    public void initQAF() {
        GherkinScenarioFactory.get().init();
    }
}

6. 解决NoSuchMethodError错误

该错误多由依赖版本不兼容导致:

  • 确保QAF与Cucumber版本匹配,比如QAF 3.2.0对应Cucumber 7.x版本
  • 执行mvn dependency:tree检查依赖树,排除重复或低版本的Cucumber相关依赖

7. 修复资源路径不存在问题

  • 确认CSV、YAML文件路径为classpath相对路径,比如testdata/user_login.csv
  • 检查Maven编译后,资源文件是否被复制到target/test-classes目录,若未复制,在pom.xml添加资源过滤配置:
<build>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <includes>
                <include>**/*.yaml</include>
                <include>**/*.csv</include>
                <include>**/*.feature</include>
            </includes>
        </testResource>
    </testResources>
</build>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 16:31:16