自己寫了一個testng執行excel用例的小程序,主要是運行.xlsx的,需要支持xls可以自己擴展,分享一下。下載地址:http://yun.baidu.com/share/link?shareid=3811093173&uk=925574576&third=0
需要引用的jar包有(demo里面也有這些jar包):

1、讀取excel
excel的數據放入List<Map<String, String>>中。這里,不包括excel第一條數據,因為第一條數據要作為map的key值。
excel格式:
package com.milan.utils; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ReadExcel { public static List<Map<String, String>> readXlsx(String fileName) { XSSFWorkbook xssfWorkbook=null; try { xssfWorkbook = new XSSFWorkbook(fileName); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 循環工作表Sheet XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0); List<Map<String, String>> list = new ArrayList<Map<String, String>>(); // 循環行Row XSSFRow rowTitleRow =xssfSheet.getRow(0); for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) { XSSFRow xssfRow = xssfSheet.getRow(rowNum); if (xssfRow == null) { continue; } Map<String, String> map = new HashMap<String, String>(); // 循環列Cell for (int cellNum = 0; cellNum <rowTitleRow.getLastCellNum(); cellNum++) { XSSFCell xssfCell = xssfRow.getCell(cellNum); XSSFCell xssfCellTitleCell = rowTitleRow.getCell(cellNum); map.put(getValue(xssfCellTitleCell), getValue(xssfCell)); } list.add(map); } return list; } @SuppressWarnings("static-access") private static String getValue(XSSFCell xssfCell) { if (xssfCell ==null){return ""; } if (xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN) { return String.valueOf(xssfCell.getBooleanCellValue()); } else if (xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC) { return String.valueOf(xssfCell.getNumericCellValue()); } else { return String.valueOf(xssfCell.getStringCellValue()); } } }
2、解析excel的數據
excel中,這個字段的值為y表示需要執行測試用例,如果為其他的,則表示不執行。
字段中{$d}開頭的表示用例說明。{$p}開頭的,表示用例需要的預置參數。比如QQ好友發送消息,但是發送消息需要先登錄,所以這里可以放登錄的用戶名和密碼。
package com.milan.utils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CaseHelper { //根據excel的map 轉換為數組 第一個為 入參 map 第二個為用例說明,第三個參數為執行用例的預置條件 public static Object[] getObjArrByMap(Map<String,String> caseExcelMap){ Map<String,String> caseParam = new HashMap<String,String>(); Map<String,String> caseDesc = new HashMap<String,String>(); Map<String,String> casePreset =new HashMap<String,String>(); CaseInfo ci = new CaseInfo(); for (String key : caseExcelMap.keySet()) { if (key.indexOf("{$d}")== 0){ caseDesc.put(key.replace("{$d}", ""), caseExcelMap.get(key)); } else if(key.indexOf("{$p}") == 0){ casePreset.put(key.replace("{$p}", ""), caseExcelMap.get(key)); } else { String strValue = caseExcelMap.get(key); if (!strValue.equals("")){ caseParam.put(key, strValue); } } } ci.setCaseDesc(caseDesc); ci.setCaseParam(caseParam); ci.setCasePreset(casePreset); return new Object[]{ci}; } ///根據excel獲取的list轉換為 Object[][] public static Object[][] getObjArrByList(List<Map<String,String>> caseExcelList){ List<Map<String,String>> caseExcuteList = getExcuteList(caseExcelList); Object[][] objArray = new Object[caseExcuteList.size()][]; for(int i = 0;i<caseExcuteList.size();i++){ objArray[i]=getObjArrByMap(caseExcuteList.get(i)); } return objArray; } ///賽選出需要執行的用例 private static List<Map<String,String>> getExcuteList(List<Map<String,String>> caseExcelList){ List<Map<String,String>> list = new ArrayList<Map<String,String>>(); for( Map<String,String> m : caseExcelList){ String str = m.get("{$d}isexcute").trim().toLowerCase(); if (str.equals("y")){ list.add(m); } } return list; } }
3、用例類
用例類有3個屬性,分別是參數,用例說明,預置參數。
package com.milan.utils; import java.util.Map; public class CaseInfo { ///{$d}isexcute 為y的時候表示需要執行 //用例參數 在excel中知己以字段名開頭 private Map<String,String> caseParam; //用例說明 在excel中以{$d}開頭 private Map<String,String> caseDesc; //用例預置條件 在excel中以{$p}開頭 private Map<String,String> casePreset; public Map<String, String> getCaseParam() { return caseParam; } public void setCaseParam(Map<String, String> caseParam) { this.caseParam = caseParam; } public Map<String, String> getCaseDesc() { return caseDesc; } public void setCaseDesc(Map<String, String> caseDesc) { this.caseDesc = caseDesc; } public Map<String, String> getCasePreset() { return casePreset; } public void setCasePreset(Map<String, String> casePreset) { this.casePreset = casePreset; } }
4、運行
package com.milan.test; import java.io.IOException; import java.util.List; import java.util.Map; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import com.milan.utils.CaseHelper; import com.milan.utils.CaseInfo; import com.milan.utils.ReadExcel; public class MyTest { protected String caseExcelPath =System.getProperty("user.dir")+"\\excel\\temp.xlsx"; @DataProvider(name = "dataInfo") protected Object[][] dataInfo1() throws IOException { Object[][] myObj = null; List<Map<String, String>> list = ReadExcel.readXlsx(caseExcelPath); myObj = CaseHelper.getObjArrByList(list); return myObj; } @Test(dataProvider="dataInfo") public void testByExcel_Body(CaseInfo c) throws IOException{ ///獲取用例說明 System.out.println(c.getCaseDesc()); ///獲取用例需要的參數 System.out.println(c.getCaseParam()); //獲取執行用例需要的前置條件 System.out.println(c.getCasePreset()); } }
5、輸出結果:
{caseExpect=1, isexcute=y, caseDesc=發送消息}
{sendname=發送者名稱, send=發送消息}
{login=登錄字符串}
讀取到excel的值之后,就可以自己加斷言,自己去請求數據調方法等等。
testng斷言失敗,繼續執行 http://blog.csdn.net/m1011566442/article/details/52084896
testng代碼執行 https://www.cnblogs.com/digod/p/6035177.html
public class Test2 { public static void main(String[] args) { //DefaultTest defaultTest = new DefaultTest(); TestNG testNG = new TestNG(); testNG.setTestClasses(new Class[]{DefaultTest.class}); testNG.run(); } }
