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

如何在QAF-Cucumber中无需定义参数即可访问测试数据文件值

QAF-Cucumber 无参数获取外部CSV测试数据实现方案

核心原理

QAF-Cucumber在解析Scenario Outline的外部数据文件时,会自动将CSV每行的列名和对应值加载到**测试上下文(TestContext)**中。无需在Feature步骤或方法定义中显式声明参数,直接通过测试上下文即可获取对应数据。

步骤定义修改实现

首先导入QAF的TestBase类,然后在步骤方法中通过测试上下文获取CSV字段值:

import com.qmetry.qaf.automation.testng.TestBase;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import com.qmetry.qaf.automation.step.QAFTestStep;

public class StepDefs {

    @Given("User is in the registration page")
    @QAFTestStep(description="User is in the registration page")
    public void userIsInRegistrationPage() {
        // 此处实现进入注册页的业务逻辑
    }

    @When("user fills the registration data")
    @QAFTestStep(description="user fills the registration data")
    public void userFillsTheRegistrationData(){
        // 从测试上下文直接提取CSV对应列的值
        String fName = TestBase.getTestContext().getProperty("FirstName").toString();
        String lName = TestBase.getTestContext().getProperty("LastName").toString();
        String title = TestBase.getTestContext().getProperty("Title").toString();
        String address = TestBase.getTestContext().getProperty("Address").toString();
        String city = TestBase.getTestContext().getProperty("City").toString();
        String zip = TestBase.getTestContext().getProperty("Zip").toString();
        String state = TestBase.getTestContext().getProperty("State").toString();

        System.out.println("FirstName == " + fName);
        System.out.println("LastName == " + lName);
        System.out.println("Title == " + title);
        System.out.println("Address == " + address);
        System.out.println("City == " + city);
        System.out.println("ZIP == " + zip);
        System.out.println("State == " + state);
    }
}

关键说明

  1. 上下文取值规则:TestBase.getTestContext().getProperty(列名)直接获取对应值,列名需与CSV表头完全一致(大小写敏感)。
  2. Feature文件无需调整:保持原有的Scenario Outline和Examples配置即可,不用在步骤中添加任何参数占位符。
  3. 类型转换:getProperty()返回Object类型,可根据实际需求转换为String、Integer等对应数据类型。

预期运行结果

执行测试后,控制台会按CSV每行数据依次输出:

FirstName == TestFName1
LastName == TestLName1
Title == Mr
Address == TestAddress1
City == TestCity1
ZIP == 11111
State == TestState1

FirstName == TestFName2
LastName == TestLName2
Title == Mr
Address == TestAddress2
City == TestCity2
ZIP == 22222
State == TestState2

FirstName == TestFName3
LastName == TestLName3
Title == Mr
Address == TestAddress3
City == TestCity3
ZIP == 33333
State == TestState3

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 10:55:37