数据驱动,用poi组件读取excel中的用例,放到一个二维数组,用于TestNG框架 dataProviderClass作为测试数据;
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
用例模板:
执行结果:
package com.test.dataprovider;
import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
public static void main(String[] args) throws Exception {
Object[][] objects = ExcelUtils.getData("E:\\yuntu-retail/fly.xlsx", "customermodify");
System.out.println(Arrays.deepToString(objects));
for (Object[] data : objects) {
for (Object datas : data) {
System.out.print(datas + "\t");
}
System.out.println();
}
}
public static Object[][] getData(String excelfilepath,String sheetname) throws Exception {
FileInputStream fileInputStream=new FileInputStream(new File(excelfilepath));
Workbook workbook=null;
String filextensionName=excelfilepath.substring(excelfilepath.indexOf("."));
System.out.println("文件:"+filextensionName);
if (filextensionName.endsWith(".xlsx")) {
workbook=new XSSFWorkbook(fileInputStream);
}else if (filextensionName.endsWith(".xls")) {
workbook=new HSSFWorkbook(fileInputStream);
}else {
System.out.println("excel文件错误");
}
Sheet sheet=workbook.getSheet(sheetname);
int rowcount=sheet.getLastRowNum()-sheet.getFirstRowNum();
List<String[]> records=new ArrayList<>();
for (int i = 1; i <=rowcount; i++) {
Row row=sheet.getRow(i);
String fields[]=new String[row.getLastCellNum()];
String ifrun=row.getCell(row.getLastCellNum()-1).getStringCellValue();
switch (ifrun) {
case ("y"):
System.out.println(
"No:" + row.getRowNum() + "Row" + "\t" + "casenumber:" + "\t" + row.getCell(0) + "will be tested");
for (int j = 0; j < row.getLastCellNum(); j++) {
row.getCell(j, MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellType(CellType.STRING);
fields[j] = row.getCell(j, MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
}
records.add(fields);
break;
case ("n"):
System.out.println(
"No:" + row.getRowNum() + "Row"+ "skip test" + "\t" + "casenumuber:" +"\t"+ row.getCell(0) + "Not Run");
break;
case ("pass"):
System.out.println(
"No:" + row.getRowNum() + "Row"+ "skip test" + "\t" + "casenumuber:" +"\t"+ row.getCell(0) + "Not Run");
break;
case ("fail"):
System.out.println("No:" + row.getRowNum() + "Row" + "\t" + "casenumuber:" + "\t"+ row.getCell(0)
+ "Executed(failed), please reset");
break;
default:
System.out.println("No:" + row.getRowNum() + "Row" + "\t" + "casenumuber:" + "\t"+ row.getCell(0)
+ "Execution result design error, please check");
break;
}
}
Object[][] results=new String[records.size()][];
for (int i = 0; i < results.length; i++) {
results[i]=records.get(i);
}
return results;
}
}
package com.test.dataprovider;
import java.lang.reflect.Method;
import org.testng.annotations.DataProvider;
public class DataProviderClass {
public static String excelpath;
public String getExcelpath() {
return excelpath;
}
public static void setExcelpath(String excelpath) {
DataProviderClass.excelpath = excelpath;
}
@DataProvider(name="CommonData")
public Object[][] GetTestData(Method method) throws Exception {
Object[][] result=ExcelUtils.getData(getExcelpath(), method.getName());
return result;
}
}
package com.test.dataprovider;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.testng.annotations.BeforeSuite;
public class NewTest1 <T extends Object>{
static String excelpath="E:\\yuntu-retail/fly.xlsx";
@BeforeSuite
public void BeforeSuite() {
DataProviderClass.excelpath="E:\\yuntu-retail/fly.xlsx";
}
@Test(dataProvider = "CommonData", dataProviderClass=DataProviderClass.class)
public void customermodify(Object ...params ) {
List<Object> list =new ArrayList<>();
for (Object t : params) {
list.add(t);
}
System.out.println(list);
System.out.println("\n");
}
}