最近在开展接口测试的自动化,目前已经实现了用xml文件作为数据驱动,下面主要是针对excel文件作为数据驱动的方法,这里面涉及到4个java文件;Idata.java(该文件主要提供统一的接口),BaseExcelData.java(把excel的数据取出来放到list中,在把list中的数据放到Object[][]数组中),ExcelDataProvider.java,Exceltest.java;
Idata.java:
package test.java.data;
/**
* @author bjlijia
* @version 1.0.0
*/
public interface IData {
public Object[][] getData(String caseName, String dataFile);
public Object[][] getData(String caseName, String dataFile,int colNum) ;
public Object[][] getData(String caseName, String dataFile,int beginNum,int endNum) ;
}
BaseExcelData.java:
package test.java.data;
import java.io.*;
import java.util.ArrayList;
import jxl.*;
import jxl.read.biff.BiffException;
/**
* @author kaixie
* @version 1.0.0
*/
public class BaseExcelData implements IData{
String defaultPath = "data/";
public Object[][] getData(String caseName, String dataFile){
return getData(caseName, dataFile, 0);
}
public Object[][] getData(String caseName, String dataFile, int rowNum) {
Object[][] data = null;
try {
data = addList(caseName, dataFile, rowNum);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
public Object[][] getData(String caseName, String dataFile, int beginRowNum, int endRowNum) {
Object[][] data = null;
try {
data = addList2(caseName, dataFile, beginRowNum, endRowNum);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
private Object[][] addList(String caseName,String dataFile,int rowNum) throws FileNotFoundException{
ArrayList list = new ArrayList();
// 文件路径
InputStream is = new FileInputStream(defaultPath + dataFile);
Object[][] data = null;
try {
Workbook wb = Workbook.getWorkbook(is);
Sheet rs = wb.getSheet(caseName);
//获取表格总行数
int rsRows = rs.getRows();
//获取表格总列数
int rsColumns = rs.getColumns();
if(rs!=null){
for (int i = 1; i <= rsRows-1; i++){
for (int j=0; j <= rsColumns-1; j++){
Cell c = rs.getCell(j, i);
String cz = c.getContents();
list.add(cz);
}
}
//System.out.println(list);
}
if (rowNum <= 0 || rowNum >= rsRows) {
data = new Object[rsRows-1][rsColumns];
int k = -1;
for (int i = 0; i < rsRows-1; i++) {
for (int j = 0; j < rsColumns; j++) {
if (k < list.size())
k++;
data[i][j] = list.get(k);
//System.out.println("i="+i+","+"j="+j+","+data[i][j]);
}
}
} else {
int k = -1;
data = new Object[rowNum][rsColumns];
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < rsColumns; j++) {
if (k < list.size())
k++;
if (i <= (rowNum - 1)) {
data[i][j] = list.get(k);
}
}
}
}
wb.close();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
private Object[][] addList2(String caseName, String dataFile, int beginNum,
int endNum) throws FileNotFoundException {
ArrayList list = new ArrayList();
// 文件路径
InputStream is = new FileInputStream(defaultPath + dataFile);
Object[][] data = null;
try {
Workbook wb = Workbook.getWorkbook(is);
Sheet rs = wb.getSheet(caseName);
//获取表格总行数
int rsRows = rs.getRows();
//获取表格总列数
int rsColumns = rs.getColumns();
if(rs!=null){
for (int i = 1; i <= rsRows-1; i++){
for (int j=0; j <= rsColumns-1; j++){
Cell c = rs.getCell(j, i);
String cz = c.getContents();
list.add(cz);
}
}
//System.out.println(list);
}
int sub = (endNum - beginNum) + 1;
data = new Object[sub][rsColumns];
if (beginNum <= 0 || endNum > rsRows) {
if(beginNum <= 0&&endNum > rsRows){
beginNum=0;
endNum=rsRows;
data = new Object[rsRows][rsColumns];
for (int i = 0; i < sub; i++) {
for (int j = 0; j < rsColumns; j++) {
if (beginNum < rsRows*rsColumns)
data[i][j] = list.get(beginNum);
beginNum++;
}
}
}else if (beginNum <= 0&&endNum <= rsRows){
beginNum=0;
sub=(endNum - beginNum);
data = new Object[endNum][rsColumns];
for (int i = 0; i < sub; i++) {
for (int j = 0; j < rsColumns; j++) {
if (beginNum < sub * rsColumns)
data[i][j] = list.get(beginNum);
beginNum++;
}
}
}else {
endNum=rsRows;
sub=(endNum - beginNum) + 1;
data = new Object[sub][rsColumns];
for (int i = 0; i < sub; i++) {
for (int j = 0; j < rsColumns; j++) {
if (beginNum <=sub * rsColumns)
data[i][j] = list.get(beginNum-1);
beginNum++;
}
}
}
} else {
int k = 0;
int a=beginNum*rsColumns;
for (int i = 0; i < sub; i++) {
for (int j = 0; j < rsColumns; j++) {
if (k < sub * rsColumns)
data[i][j] = list.get(a+k-2);
k++;
}
}
}
wb.close();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
}
ExcelDataProvider.java:
package test.java.data;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;
import org.testng.annotations.DataProvider;
/**
* @author kaixie
* @version 1.0.0
*/
public class ExcelDataProvider {
// 从XML文本文件中获得数据
@DataProvider(name = "GetDataFromXml")
public static Object[][] getTestDataFromXml(Method m) throws FileNotFoundException {
// 通过反射获得函数名称,可以为多个测试方法提供数据驱动
Object[][] o = new Object[][] {};
// 取用例数据集d1Test的全部数据(excel数据源)
if (m.getName().equals("test1")) {
return new BaseExcelData().getData("testA", "DataProvider.xls",2,5);
}
if (m.getName().equals("test2")) {
return new BaseExcelData().getData("testB", "DataProvider.xls",2,5);
}
//取用例数据集d2Test的全部数据(excel数据源)
/*if (m.getName().equals("test1")) {
return new BaseExcelData.getData("d1Test","caipiaoTest.xls")
}*/
return o;
}
}
caipiaotest.java:
package test.java.testcase;
import org.testng.annotations.Test;
import test.java.data.DataProvider2;
import test.java.data.ExcelDataProvider;
public class Exceltest {
@Test(dataProvider = "GetDataFromXml", dataProviderClass = ExcelDataProvider.class)
public void test1(String gameNames, String matchId, String platformId) {
//String expected = "";
String params = "gameNames=" + gameNames + "&matchId=" + matchId
+ "&platformId=" + platformId;
//System.out.println(params);
}
@Test(dataProvider = "GetDataFromXml", dataProviderClass = ExcelDataProvider.class)
public void test2(String gameNames, String matchId, String platformId) {
//String expected = "";
String params = "gameNames=" + gameNames + "&matchId=" + matchId
+ "&platformId=" + platformId;
//System.out.println(params);
}
}