TestNg引數化測試之讀取csv檔案
阿新 • • 發佈:2019-02-03
1、新建csv文件,準備好測試資料
在當前工程的resources目錄下,新建檔名為add的csv文件
開啟csv檔案,構造n1、n2、r1三個引數資料
2、新建一個CSVData類用來獲取csv檔案中的資料
package com.mcj.testng;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
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 in;
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.resources.";
String absolutePath=directory.getCanonicalPath()+path.replaceAll("\\.", Matcher.quoteReplacement("\\"))+fileName;
System.out.println(absolutePath);
File csv=new File(absolutePath);
in=new BufferedReader(new FileReader(csv));
while (in.ready()) {
csvList.add(in.readLine());
this.rowNum++;
}
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++;
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
if(rowNum==0||curRowNo>=rowNum){
try {
in.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");
}
}
3、建立TestNg測試類CalculatorCSV
package com.mcj.testng;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class CalculatorCSV {
Calculator cal=new Calculator();
@DataProvider(name="num")
public Iterator<Object[]> Numbers() throws IOException{
return (Iterator<Object[]>)new CSVData("add.csv");
}
@Test(dataProvider="num")
public void testAdd(Map<String, String> data){
float num1=Float.parseFloat(data.get("n1"));
float num2=Float.parseFloat(data.get("n2"));
float expectedResult=Float.parseFloat(data.get("r1"));
Float actual=cal.add(num1, num2);
Assert.assertEquals(actual, expectedResult);
}
}
4、執行結果
PASSED: testAdd({n1=3, n2=2, r1=5})
PASSED: testAdd({n1=-3, n2=8, r1=5})
FAILED: testAdd({n1=5.4, n2=6.6, r1=11.9})
java.lang.AssertionError: expected [11.9] but found [12.0]
at org.testng.Assert.fail(Assert.java:94)