方式一:
第一、讀取csv數(shù)據(jù)源碼
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
public class CSVData implements Iterator<Object[]> {
BufferedReader bufreader;
ArrayList<String> csvList=new ArrayList<String>();
int rowNum=0; //行數(shù)
int columnNum=0; //列數(shù)
int curRowNo=0; //當前行數(shù)
String columnName[]; //列名
/**
* 在TestNG中由@DataProvider(dataProvider = "name")修飾的方法
* 取csv文件數(shù)據(jù)時時,調(diào)用此類構造方法(此方法會得到列名并將當前行移到下一行)執(zhí)行后,轉(zhuǎn)發(fā)到
* TestNG自己的方法中去,然后由它們調(diào)用此類實現(xiàn)的hasNext()、next()方法
* 得到一行數(shù)據(jù),然后返回給由@Test(dataProvider = "name")修飾的方法,如此
* 反復到數(shù)據(jù)讀完為止
* @param fileName
* @throws IOException
*/
public CSVData(String fileName) throws IOException{
File directory=new File(".");
String path=".src.test.resources.testdata.";
String absolutePath=directory.getCanonicalPath()+path.replaceAll("\.", Matcher.quoteReplacement("\"))+fileName;
System.out.println("路徑:"+absolutePath);
//將csv中的數(shù)據(jù)讀取到csvList中
File csv=new File(absolutePath);
bufreader=new BufferedReader(new FileReader(csv));
while (bufreader.ready()) {
csvList.add(bufreader.readLine());
this.rowNum++;
}
//獲取列名存放在columnName、列數(shù)
String st=csvList.get(0);
System.out.println("列名是:===="+st);
String[] str=csvList.get(0).split(",");
this.columnNum=str.length;
columnName=new String[columnNum];
//獲取列名
for (int i = 0; i < columnNum; i++) {
columnName[i]=str[i];
}
this.curRowNo++;
System.out.println(csvList+"======================"+columnName);
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
if(rowNum==0||curRowNo>=rowNum){
try {
bufreader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}else{
return true;
}
}
/**
* 獲取一組參數(shù),即一行數(shù)據(jù)
*/
@Override
public Object[] next() {
// TODO Auto-generated method stub
Map<String,String> s=new TreeMap<String,String>();
String csvCell[]=csvList.get(curRowNo).split(",");
for(int i=0;i<this.columnNum;i++){
s.put(columnName[i], csvCell[i]);
}
Object[] d=new Object[1];
d[0]=s;
this.curRowNo++;
return d;
}
@Override
public void remove() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("remove unsupported");
}
}
第二、testng中使用測試
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.zhongan.investmentlink.wangyi.CSVData;
public class TestCSV {
@DataProvider(name="num")
public Iterator<Object[]> Numbers() throws IOException{
String fileName="UnderWriteRequest.csv";
return (Iterator<Object[]>)new CSVData(fileName);
}
@Test(dataProvider="num")
public void testAdd(Map<String, String> data){
String num1=data.get("totalPremium");
String num2=data.get("holderName");
System.out.println(num1+"================="+num2);
}
}