【发布时间】:2017-09-17 08:17:59
【问题描述】:
我正在使用 TestNg Dataprovider 从电子表格中读取 json 数据的 Rest API 测试(POST 方法)。 我的 Dataprovider 返回带有键的 HashMap:Integer Row_Number 和值:测试数据的 ArrayList (String)。以下是 DataProvider 返回的示例地图。 {0=[Sample1, Name1, sample1.name1@example.com, (000) 111-1111], 1=[Sample2, Name2, sample2.name2@example.com, (000) 111-1112]}
我目前的 Dataprovider 实现是,
@DataProvider
public Object[][] JSONBODY()
{
String test_data = "json_data";
int row = ExcelUtils.getRowNum(test_data, col_num);
int total_col = ExcelUtils.getLastColumnNumber(row);
Map<Integer, ArrayList<String>> map = ExcelUtils.getTableArray(spreadsheet_location,test_data,total_col);
return new Object[][] { { map } };
}
getTableArray 实现
public static Map<Integer, ArrayList<String>> getTableArray(String FilePath, String testdata, int total_Col) throws Exception {
Map<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
ArrayList<Integer> iTestCaseRow = null;
try
{
FileInputStream ExcelFile = new FileInputStream(FilePath);
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
int startCol = 1;
iTestCaseRow = ExcelUtils.getRowContains(testdata ,col_num); // getRowContains returns list of row numbers for value in testdata.
int totalRows = iTestCaseRow.size();
int totalCols = total_Col;
for(int i=0; i<totalRows;i++)
{
ArrayList<String> str = new ArrayList<String>();
for (int j=startCol;j<=totalCols;j++)
{
str.add (ExcelUtils.getCellData(iTestCaseRow.get(i),j));
}
map.put(iTestCaseRow.get(i), str);
}
return map;
}
}
测试方法
@Test(dataProvider = "JSONBODY")
public void TestMethod(Map<Integer, ArrayList<String>> map) throws Exception {
try
{
Log.startTestCase("Start executing Test Case");
Set<Integer> key = map.keySet();
for(Integer row: key)
{
SamplePojo pojo = new SamplePojo();
ArrayList<String> data = map.get(row);
pojo.setFirstName(data.get(0));
pojo.setLastName(data.get(1));
pojo.setEmail(data.get(2));
pojo.setPhone(data.get(3));
Response res = RestAssured.given().contentType(ContentType).body(pojo).when().post(POST_URL);
Log.info(res.asString());
Assert.assertTrue(res.getStatusCode() == 200 , "Test Case failed");
}
}
}
电子表格测试数据是, Spreadsheet Data
当我执行 @Test 方法时,TestNG 会作为一种方法而不是两种方法执行,因为我在电子表格中有 2 行测试数据(值:json_data)。 请帮助我为每个键:值对单独运行测试方法。 提前致谢!
【问题讨论】:
标签: java testng rest-assured data-driven-tests testng-dataprovider