testng 接口测试,读取Excel表格数据中json数据,做数据驱动
public class TestRegister { //fastjson解析数据 @Test(dataProvider="datas3") public void test4(String paramter){ String url = "https://10.1.30.18:8443/demo"; Map<String,String> params = (Map<String, String>) JSONObject.parse(paramter); System.out.println(HttpUtils.doPost(params,url)); } @DataProvider public Object[][] datas3(){ //取出固定列的数据 int[] rows = {1,2,3,4,5,6}; int[] cols = {3}; Object[][] datas = ExcelUtil.readExcel2("C:\\Users\\Administrator\\Desktop\\register2.xls",rows,cols); return datas; } }
ExcelUtil.readExcel2
public static Object[] [] readExcel2(String url,int[] rows,int[] cols){ Object[] [] datas = new Object[rows.length][cols.length]; //获取Workbook对象 try { File file = new File(url); Workbook workBook = WorkbookFactory.create(file); //获取sheet 对象 Sheet sheet = workBook.getSheet("Sheet1"); DataFormatter formatter = new DataFormatter(); //获取行 for(int i = 0;i < rows.length ;i++){ Row row = sheet.getRow(rows[i]); for(int j = 0;j < cols.length;j++){ Cell cell = row.getCell(cols[j], Row.MissingCellPolicy.CREATE_NULL_AS_BLANK); String value = formatter.formatCellValue(cell); // System.out.print(value+","); datas[i][j] = value; } System.out.println(); } } catch (IOException e) { e.printStackTrace(); } return datas; }
表格
HttpUtils.doPost以及所需要jar包参考:https://www.cnblogs.com/ychun/p/15612428.html