美文网首页
[TestNG]TestNG的参数化测试实战

[TestNG]TestNG的参数化测试实战

作者: 测试你个头 | 来源:发表于2017-10-17 17:22 被阅读0次

TestNG系列:
TestNG和Junit4的参数化测试对比
TestNG运行指定测试套件
TestNG整合ReportNG
TestNG参数化测试实战
TestNG+Spring/Spring Boot整合

使用TestNG的参数化测试特性实现测试数据和用例的分离,测试参数保存在数据文件中(csv,json等格式),本次范例使用json格式


  • 一、封装BaseTest作为所有测试类的基类,定义并实现DataProvider方法
    这里DataProvider方法使用测试方法对象(Method Object)作为参数
@ContextConfiguration(locations = "classpath:oms-ci-spring.xml")
public class BaseTest extends AbstractTestNGSpringContextTests {

    @DataProvider(name = "readTestData")
    public Object[][] testData(Method testMethod) {

        if (testMethod.getName().equals("testChooseShift")) {
            return new Object[][]{new Object[]{135001}};
        }

        String className = testMethod.getDeclaringClass().getSimpleName();

        JSONObject paramJson = readParam(className);
        JSONArray paramArr = paramJson.getJSONArray("data");

        Object[][] param = new Object[paramArr.size()][];
        for (int i = 0; i < paramArr.size(); i++) {
            JSONArray childArr = paramArr.getJSONArray(i);
            param[i] = new Object[childArr.size()];
            for (int j = 0; j < childArr.size(); j++) {
                param[i][j] = childArr.get(j).toString();
            }
        }

        return param;
    }

    /**
     *
     * @param className
     * @return
     */
    public JSONObject readParam(String className) {
        String paramText = this.readParamFile(className);

        JSONObject paramJson = null;

        try {
            paramJson = (JSONObject) JSONObject.parse(paramText);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return paramJson;
    }

    /**
     *
     * @param fileName
     * @return
     */
    public String readParamFile(String fileName) {

        String fileUrl = String.join("/", "param", fileName + ".json");

        return this.readFile(fileUrl);
    }

    /**
     *
     * @param fileUrl
     * @return
     */
    public String readFile(String fileUrl) {
        InputStream fi = null;
        BufferedReader bo = null;
        try {
            fi = this.getClass().getClassLoader().getResourceAsStream(fileUrl);
            bo = new BufferedReader(new InputStreamReader(fi, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        StringBuilder stB = new StringBuilder();
        try {
            String lineRead = "";
            while ((lineRead = bo.readLine()) != null) {
                stB.append(lineRead);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                fi.close();
                bo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return stB.toString();
    }
}
  • 二、测试方法通过DataProvider引用参数读取的方法
/**
     * @param calDate  indicator的date_Str
     * @param targetId xspace的servicerId
     * @param expect expectValue
     */
    @Test(dataProvider = "readTestData")
    public void calIndicatorTest(String calDate, String targetId, String expect) {
        ......
        }
    }
  • 三、在test/resources/目录下创建数据文件,要求文件名称和测试类名称一致:

这里可以按照用例目录的结构来组织数据文件
数据文件格式如下,param1~3对应用例需要参数

{
  "data": [
    [
      param1,
      param2,
      param3
    ]
  ]
}

举例:第二步中public void calIndicatorTest(String calDate, String targetId, String expect)的实际参数如下:

{
  "data": [
    [
      "20170831",
      98000337284,
      {
        "1": "82.74",
        "2": "55.35",
        "3": "85.59",
        "4": "77.76",
        "5": "84.21",
        "6": "46.43",
        "7": "87.32",
        "8": "75.913",
        "10": "10.94",
        "12": "58.26",
        "14": "11.31",
        "15": "10.53"
      }
    ]
  ]
}

通过以上三步,就可以实现数据和用例分离原型

相关文章

网友评论

      本文标题:[TestNG]TestNG的参数化测试实战

      本文链接:https://www.haomeiwen.com/subject/hmihdxtx.html