我试图通过以 TestNG DataProvider 的形式编写可重用组件来最小化测试中的代码行。需要发送到服务器的我的测试规范接受 Map>。
@Test(dataProvider = "provideData")
public void TestMethod(Map<String,Object> map) throws Exception {
RequestSpecification spec = generateCommonReqSpecJsonWithQueryParams(map);
Response res = RestOperationUtils.get(url, spec, null);
}
@DataProvider(name="provideData")
public static Object[][] getData() throws Exception {
Map<String, ArrayList<String>> map = new HashMap<>();
ArrayList<String> a1 = new ArrayList<>();
a1.add("First Value");
a1.add("Second Value);
a1.add("Third Value");
a1.add("Fourth Value");
map.put("Test[]", a1);
map.put("month_start", new ArrayList(Arrays.asList("2019-06-01")));
map.put("month_end", new ArrayList(Arrays.asList("2019-06-30")));
map.put("viewers[]", new ArrayList(Arrays.asList("ESPN")));
ArrayList<String> b1 = new ArrayList<>();
b1.add("Fifth Value");
b1.add("Sixth Value");
b1.add("Seventh Value");
map.put("Result[]", b1);
由于 TestNG 要求我们从 DataProvider 返回 Object[][],以下是我尝试过的不同方法:
方法一:
String[] keys = new String[map.size()];
ArrayList<ArrayList<String>> values = new ArrayList<>();
int index = 0;
for (Map.Entry<String, ArrayList<String>> mapEntry : map.entrySet()) {
keys[index] = mapEntry.getKey();
values.add(index, (ArrayList<String>) mapEntry.getValue());
// x[index] = mapEntry.getValue();
index++;
}
Object[][] result = new Object[values.size()][];
index = 0;
int index2;
for (List<String> list : values) {
result[index] = new Object[list.size()];
index2 = 0;
for (String item : list) {
result[index][index2] = item;
index2++;
}
index++;
}
return result ;
HUH函数
相关分类