从testson

时间:2017-07-17 14:10:30

标签: java arrays json testng testng-dataprovider

我想使用json文件在dataProvider中提供数据。请为它建议最好的方法,(在java。中)

例如。 Json文件

{
  "dataSet": [
    {
      "testCase": "Verify error message on wrong userName",
      "username": "test",
      "password": "password",
      "rememberMe": false
    },
    {
      "testCase": "Verify error message on empty userName",
      "username": "",
      "password": "password",
      "rememberMe": false
    }
  ]
}

理想情况下,数据提供商应该看起来像

@DataProvider(name = "dpForIncorrectUsernameOrPassword")
public static Object[][] incorrectUsernameOrPassword() {
    Object[][] testObjArray = JsonUtils.getJsonObjects("test.json");
    return testObjArray;
}

那样

{
  "testCase": "Verify error message on wrong userName",
  "username": "test",
  "password": "password",
  "rememberMe": false
}

这是一个数据集,

{
  "testCase": "Verify error message on empty userName",
  "username": "",
  "password": "password",
  "rememberMe": false
}

行动另一个,依此类推......

1 个答案:

答案 0 :(得分:2)

这是你怎么做的。

表示Json文件中每个数据集的POJO类如下所示:

public class TestData {
    private String testCase;
    private String username;
    private String password;
    private boolean rememberMe;

    public String getTestCase() {
        return testCase;
    }

    public void setTestCase(String testCase) {
        this.testCase = testCase;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isRememberMe() {
        return rememberMe;
    }

    public void setRememberMe(boolean rememberMe) {
        this.rememberMe = rememberMe;
    }

    @Override
    public String toString() {
        return "TestData{" +
                "testCase='" + testCase + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", rememberMe=" + rememberMe +
                '}';
    }
}

以下是您的测试类的外观:

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;

public class SampleTestClass {

    @Test(dataProvider = "getData")
    public void testMethod(TestData data) {
        System.out.println(data);
    }

    @DataProvider
    public Object[][] getData() throws FileNotFoundException {
        JsonElement jsonData = new JsonParser().parse(new FileReader("src/test/resources/45146523.json"));
        JsonElement dataSet = jsonData.getAsJsonObject().get("dataSet");
        List<TestData> testData = new Gson().fromJson(dataSet, new TypeToken<List<TestData>>() {
        }.getType());
        Object[][] returnValue = new Object[testData.size()][1];
        int index = 0;
        for (Object[] each : returnValue) {
            each[0] = testData.get(index++);
        }
        return returnValue;
    }
}

<强> PS 在这里,我正在使用Google Gson