java数据驱动测试_zhuanti0905
1. 数据格式:

2. excel处理方法:
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ExcelDataHandle {
public static List<HashMap<String,String>> ExcelReader(String excelPath,String sheetName) throws IOException {
//创建输入流
InputStream inputStream = new FileInputStream(excelPath);
//创建07版excel工作簿
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
//获取sheet页
XSSFSheet sheet = workbook.getSheet(sheetName);
//获取标题行
String[] title = null;
title = new String[sheet.getRow(0).getLastCellNum()];
for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) {
title[i] = sheet.getRow(0).getCell(i).toString();
}
//获取用例行列表
List<HashMap<String, String>> caseInfo = new ArrayList<>();
for (int i=1; i<= sheet.getLastRowNum(); i++){
HashMap<String,Object> temp = new HashMap<>();
for (int j = 0; j < sheet.getRow(i).getLastCellNum(); j++) {
temp.put(title[j],sheet.getRow(i).getCell(j).toString());
}
caseInfo.add((HashMap<String, String>)temp.clone());
temp.clear();
}
//关闭文件流
inputStream.close();
return caseInfo;
}
}
3. 用例信息类:
public class CaseInfo {
private String caseNo;
private String caseName;
private String apiName;
private String apiUrl;
private String apiParam;
private String caseIsExec;
private String caseDesc;
public CaseInfo(){}
public CaseInfo(String caseNo, String caseName, String apiName, String apiUrl, String apiParam, String caseIsExec, String caseDesc) {
this.caseNo = caseNo;
this.caseName = caseName;
this.apiName = apiName;
this.apiUrl = apiUrl;
this.apiParam = apiParam;
this.caseIsExec = caseIsExec;
this.caseDesc = caseDesc;
}
public String getCaseNo() {
return caseNo;
}
public void setCaseNo(String caseNo) {
this.caseNo = caseNo;
}
public String getCaseName() {
return caseName;
}
public void setCaseName(String caseName) {
this.caseName = caseName;
}
public String getApiName() {
return apiName;
}
public void setApiName(String apiName) {
this.apiName = apiName;
}
public String getApiUrl() {
return apiUrl;
}
public void setApiUrl(String apiUrl) {
this.apiUrl = apiUrl;
}
public String getApiParam() {
return apiParam;
}
public void setApiParam(String apiParam) {
this.apiParam = apiParam;
}
public String getCaseIsExec() {
return caseIsExec;
}
public void setCaseIsExec(String caseIsExec) {
this.caseIsExec = caseIsExec;
}
public String getCaseDesc() {
return caseDesc;
}
public void setCaseDesc(String caseDesc) {
this.caseDesc = caseDesc;
}
4. 用例信息处理器:
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class CaseInfoFilter {
public static List<CaseInfo> generateCaseObjectList(String excelPath,String sheetName) throws IOException {
List <HashMap<String,String>> dataInfo = ExcelDataHandle.ExcelReader(excelPath,sheetName);
List<CaseInfo> caseObjectList = new ArrayList<>();
for (HashMap<String, String> data : dataInfo) {
if (data.get("是否执行").equals("Y")) {
CaseInfo caseObj = new CaseInfo();
caseObj.setCaseNo(data.get("用例编号"));
caseObj.setCaseName(data.get("用例名称"));
caseObj.setApiName(data.get("api名称"));
caseObj.setApiUrl(data.get("apiUrl"));
caseObj.setApiParam(data.get("api参数"));
caseObj.setCaseDesc(data.get("用例描述"));
caseObjectList.add(caseObj);
}
}
return caseObjectList;
}
}
5. 数据驱动器:
import org.testng.annotations.DataProvider;
import java.io.IOException;
import java.util.List;
public class DataProviderMethod {
public static String excelPath = "D:\\Code\\testfile.xlsx";
public static String sheetName = "SAM_APIFun";
@DataProvider(name="dataProvider1")
public static Object[][] DataProvider1() throws IOException {
List<CaseInfo> caseObjectList = CaseInfoFilter.generateCaseObjectList(excelPath, sheetName);
int listLength = caseObjectList.size();
Object[][] caseObj = new Object[listLength][1];
for (int i = 0; i < listLength; i++) {
caseObj[i][0] = caseObjectList.get(i);
}
return caseObj;
}
}
6. 测试用例:
public class AppTest
{
@Test(dataProvider="dataProvider1",dataProviderClass = DataProviderMethod.class)
public void runTest1(CaseInfo info){
System.out.println("============"+info.getCaseNo()+"============");
System.out.println(info.getCaseName());
System.out.println(info.getApiName());
System.out.println(info.getApiUrl());
System.out.println(info.getApiParam());
System.out.println(info.getCaseDesc());
}
}
7. 获取当前类文件的绝对路径:
public String generateFilePath(){
return (new File(this.getClass().getResource("").getPath())).toString();
}

浙公网安备 33010602011771号