关于Java自动化测试csv数据的读取

1.新建add.csv,准备好测试数据:

n1,n2,r1

3,2,5

5.4,6.6,11.9

2.新建一个Calculator类实现如下功能:

public class Calculator {

public Float add(Float num1,Float num2){

return num1+num2;

}

}

3.新建一个CsvUtils类用来读取csv中的数据:

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 CsvUtils implements Iterator {

BufferedReader in;

ArrayList csvList=new ArrayList();

int rowNum=0; //行数

int columnNum=0; //列数

int curRowNo=0; //当前行数

String columnName[]; //列名

/**

* 在TestNG中由@DataProvider(dataProvider = "name")修饰的方法取csv数据时,

* 调用此类构造方法(此方法会得到列名),

* 返回给由@Test(dataProvider = "name")修饰的方法,如此

* 反复到数据读完为止

* @param fileName 文件名

* @throws IOException

*/

public CsvUtils(String fileName) throws IOException{

File directory=new File(".");

String path=".src.main.java.page.testdata."; //文件路径

String absolutePath=directory.getCanonicalPath()+path.replaceAll("\\.", Matcher.quoteReplacement("\\"))+fileName;

System.out.println(absolutePath); //打印路径

File csv=new File(absolutePath);

in=new BufferedReader(new FileReader(csv)); //读取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 s=new TreeMap();

String csvCell[]=csvList.get(curRowNo).split(",");

for(int i=0;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");

}

}

4. 新建TestNg测试类CsvTest:

import java.io.IOException;

import java.util.Iterator;

import java.util.Map;

import core.utils.CsvUtils;

import org.testng.Assert;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

public class CsvTest{

Calculator cal=new Calculator();

@DataProvider(name="num")

public Iterator Numbers() throws IOException{

return (Iterator)new CsvUtils("add.csv");

}

@Test(dataProvider="num")

public void testAdd(Map 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);

}

}

5.执行结果:

passed:testAdd({n1=3,n2=2,r1=5})

failed:testAdd({n1=5.4,n2=6.6,r1=11.9})

java.lang.AssertionError:expected [11.9] but found [12.0]

文章来源:千锋软件测试


转载于:https://juejin.im/post/5cdd385cf265da035e214c37

你可能感兴趣的:(关于Java自动化测试csv数据的读取)