方式一:
第一、读取csv数据源码
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; //行数 int columnNum=0; //列数 int curRowNo=0; //当前行数 String columnName[]; //列名 /** * 在TestNG中由@DataProvider(dataProvider = "name")修饰的方法 * 取csv文件数据时时,调用此类构造方法(此方法会得到列名并将当前行移到下一行)执行后,转发到 * TestNG自己的方法中去,然后由它们调用此类实现的hasNext()、next()方法 * 得到一行数据,然后返回给由@Test(dataProvider = "name")修饰的方法,如此 * 反复到数据读完为止 * @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中的数据读取到csvList中 File csv=new File(absolutePath); bufreader=new BufferedReader(new FileReader(csv)); while (bufreader.ready()) { csvList.add(bufreader.readLine()); this.rowNum++; } //获取列名存放在columnName、列数 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; } } /** * 获取一组参数,即一行数据 */ @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); } }
方式二:
package com.hct.Job;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestCSV {
//读取CSV文件的静态方法,使用CSV文件的绝对文件路径作为函数参数
public static Object[][] getSearchData(String FileName) throws IOException{
List<Object[]> records=new ArrayList<Object[]>();
String record;
//设定UTF-8字符集,使用带缓冲区的字符输入流BufferedReader读取文件内容
BufferedReader file=new BufferedReader(new InputStreamReader(new FileInputStream(FileName),"UTF-8"));
//忽略读取CSV文件的标题行(第一行)
file.readLine();
//遍历读取文件中除第一行外的其他所有内容并存储在名为records的ArrayList中,每一行records中存储的对象为一个String数组
while((record=file.readLine())!=null){
String fields[]=record.split(",");
// System.out.println(fields);
records.add(fields);
}
//关闭文件对象
file.close();
//将存储测试数据的List转换为一个Object的二维数组
Object[][] results=new Object[records.size()][];
//设置二位数组每行的值,每行是一个Object对象
for(int i=0;i<records.size();i++){
results[i]=records.get(i);
}
return results;
}
@DataProvider(name="searchData")
public static Object[][] data() throws IOException
{
String filename ="D:\\eclipse-workspace\\Job\\src\\test\\java\\com\\hct\\Job\\demo.csv";
return getSearchData(filename);//获取CSV文件的测试数据
}
@Test(dataProvider="searchData")
public void testData(String n1,String n2,String n3){
System.out.println(n1+"+++++++++++++++++++++++++++++"+n2+"+++++++++++++++++"+n3);
}
}