testNg自动化,读取excel的数据
自己寫了一個testng執(zhí)行excel用例的小程序,主要是運行.xlsx的,需要支持xls可以自己擴展,分享一下。下載地址:http://yun.baidu.com/share/link?shareid=3811093173&uk=925574576&third=0
需要引用的jar包有(demo里面也有這些jar包):
1、讀取excel
??? excel的數(shù)據(jù)放入List<Map<String, String>>中。這里,不包括excel第一條數(shù)據(jù),因為第一條數(shù)據(jù)要作為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();}// 循環(huán)工作表SheetXSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);List<Map<String, String>> list = new ArrayList<Map<String, String>>();// 循環(huán)行RowXSSFRow 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>();// 循環(huán)列Cellfor (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的數(shù)據(jù)
?? excel中,這個字段的值為y表示需要執(zhí)行測試用例,如果為其他的,則表示不執(zhí)行。
? 字段中{$d}開頭的表示用例說明。{$p}開頭的,表示用例需要的預置參數(shù)。比如QQ好友發(fā)送消息,但是發(fā)送消息需要先登錄,所以這里可以放登錄的用戶名和密碼。
?
package com.milan.utils;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CaseHelper {//根據(jù)excel的map 轉換為數(shù)組 第一個為 入?yún)?map 第二個為用例說明,第三個參數(shù)為執(zhí)行用例的預置條件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};}///根據(jù)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;}///賽選出需要執(zhí)行的用例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個屬性,分別是參數(shù),用例說明,預置參數(shù)。
package com.milan.utils;import java.util.Map;public class CaseInfo {///{$d}isexcute 為y的時候表示需要執(zhí)行//用例參數(shù) 在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());///獲取用例需要的參數(shù) System.out.println(c.getCaseParam());//獲取執(zhí)行用例需要的前置條件 System.out.println(c.getCasePreset());}}
5、輸出結果:
讀取到excel的值之后,就可以自己加斷言,自己去請求數(shù)據(jù)調(diào)方法等等。
testng斷言失敗,繼續(xù)執(zhí)行?http://blog.csdn.net/m1011566442/article/details/52084896
?testng代碼執(zhí)行??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();} } View Code?
轉載于:https://www.cnblogs.com/milanmi/p/4636512.html
總結
以上是生活随笔為你收集整理的testNg自动化,读取excel的数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国防知识|中国九大最新军事武器
- 下一篇: KMP POJ 3461 Oulipo