Dataprovider 与 xls 中每行的验证函数存在问题

问题描述 投票:0回答:1

我有以下课程

pulbic class DataProiders {

@DataProvider(name="data")
public String[][] getAllData() throws IOException {
// Using XLUtils class which uses XSSWorkbook to get cell data
//Take the  name of the xls file and sheet name and calculate row and column count
//two for loops rowcount and columncount to get all data and return the data
}

//I have the test clsss test.java
public class CutomerRecords {

pulbic Response response;
public static JSONObject resJsonObj;

@Test(priority = 1, dataProvider = "data", dataProviderClass = DataProviders.class)
public void CustomerData(String cId, String fName, String lName, String email, String postCode){
//These are the 5 columns in the xls sheet
//set method to set all the parameters using pojo class for request

//Call the POST API and get the response back
response = PostMehtodFromAPIClass(request)
resJsonOj= new JSONObject(response.getBody().asString());
}

@Test(priority=2)
public void validateResponseCusID(){
//my validation logic using resJsonObj
}

@Test(priority=2)
public void validateResponseCusName(){
//my validation logic using resJsonObj
}

@Test(priority=2)
public void validateResponseCusPostCode(){
//my validation logic using resJsonObj
}

@Test(priority=2)
public void validateResponseCusEmail(){
//my validation logic using resJsonObj
}

在这里,我的要求是首先对 xls 表中的第一行执行 CustomerData 并继续每个验证响应测试函数,然后读取第二行并测试验证响应。

但是发生的情况是,如果我在 xls 中有 5 行,它会执行 5 行,并且只有最后一行验证响应函数才起作用。我需要建议一次读取一行并调用验证函数,然后调用第二行和验证,依此类推。

java testng xls testng-dataprovider
1个回答
0
投票

问题出在您的测试代码中。您有一个静态数据成员,它将记住最后一次分配。这解释了为什么其他两个测试基本上只在最后一次迭代中执行。

你需要的是一个TestNG工厂。

这是一个展示如何与工厂合作的示例。

public record Student(int id, String name, String address) { }
import org.testng.annotations.DataProvider;

public class DataProviders {

    @DataProvider(name = "data")
    public Object[][] getAllData() {
        return new Object[][]{
                {new Student(20, "Alpha", "Chennai")},
                {new Student(22, "Beta", "Bangalore")},
                {new Student(23, "Gamma", "Hyderabad")},
        };
    }
}
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class CustomerRecordsTest {

    private final Student student;
    private JsonObject resJsonObj;
    private static final Gson gson = new Gson();

    @Factory(dataProvider = "data", dataProviderClass = DataProviders.class)
    public CustomerRecordsTest(Student student) {
        this.student = student;
    }

    @Test(priority = 1)
    public void customerData() {
        resJsonObj = gson.fromJson(gson.toJson(student), JsonObject.class);
    }

    @Test(priority=2)
    public void validateResponseCusID(){
        System.err.println("Student id = " + resJsonObj.get("id"));
    }

    @Test(priority=3)
    public void validateResponseCusName(){
        System.err.println("Student name = " + resJsonObj.get("name"));
    }

    @Test(priority=4)
    public void validateResponseCusPostCode(){
        System.err.println("Student address = " + resJsonObj.get("address"));
    }
}

有关 TestNG 工厂的更多详细信息,请参阅:https://testng.org/#_factories

© www.soinside.com 2019 - 2024. All rights reserved.