Java用excel做单元测试_java 接口测试,使用excel做数据驱动(二)

改变我们的测试驱动方式,灵活设置测试用例。

数据驱动测试

数据驱动测试的核心是:

测试数据与测试脚本分离,实现测试脚本参数化,

提高测试脚本的可重用性。在自动化功能测试中如果灵活使用数据源与测试脚本,

便能轻松创建与运行成百上千个测试用例。自动化测试框架必须要有与电子表格、文本文件、数据库集成的能力。

首先小伙伴们就会问,你要先解析Excel吧,那你肯定得给我上代码,是的,必须的上代码。

packagecom.testapi.until;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importorg.apache.poi.xssf.usermodel.XSSFCell;importorg.apache.poi.xssf.usermodel.XSSFRow;importorg.apache.poi.xssf.usermodel.XSSFSheet;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;public classExcelUtils {private staticXSSFSheet ExcelWSheet;private staticXSSFWorkbook ExcelWBook;private staticXSSFCell Cell;private staticXSSFRow Row;public static Object[][] getTableArray(String FilePath, String SheetName) throwsException {

String[][]tabArray= null;try{

FileInputStream ExcelFile= newFileInputStream(FilePath);

ExcelWBook= newXSSFWorkbook(ExcelFile);

ExcelWSheet=ExcelWBook.getSheet(SheetName);int startRow = 1;int startCol = 1;int ci,cj = 0;int totalRows =ExcelWSheet.getLastRowNum();int totalCols = 2;

tabArray=new String[totalRows][6];

ci=0;

cj=0;int cm = 0;int cl = 0;int ch = 0;for (int i=startRow;i<=totalRows;i++, ci++) {

tabArray[ci][0]=getCellData(i,2);

tabArray[ci][1]=getCellData(i,3);

tabArray[ci][2]=getCellData(i,4);

tabArray[ci][3]=getCellData(i,5);

tabArray[ci][4]=getCellData(i,6);

}

}catch(FileNotFoundException e){

System.out.println("Could not read the Excel sheet");

e.printStackTrace();

}catch(IOException e){

System.out.println("Could not read the Excel sheet");

e.printStackTrace();

}return(tabArray);

}public static String getCellData(int RowNum, int ColNum) throwsException {try{

Cell=ExcelWSheet.getRow(RowNum).getCell(ColNum);int dataType =Cell.getCellType();if (dataType == 3) {return "";

}else{

String CellData=Cell.getStringCellValue();returnCellData;

}

}catch(Exception e){//System.out.println(e.getMessage());

throw(e);

}

}public static void main(String[] args) throwsException {

ExcelUtils excelUtils=newExcelUtils();

Object[][] m= excelUtils.getTableArray("C:\\Users\\Administrator\\eclipse-workspace\\ApiTest\\casedata\\casedata.xlsx","Sheet1");

}

}

这就是我们的代码,那么小伙伴迫不及待了,你快来告诉我,

你的测试用例怎么组织的,好的 ,热腾腾的钙素你,我给你的就是代码,就是源码。源码如下

packagecom.testapi.casev;import staticorg.testng.Assert.assertEquals;importorg.testng.annotations.AfterTest;importorg.testng.annotations.BeforeTest;importorg.testng.annotations.DataProvider;importorg.testng.annotations.Listeners;importorg.testng.annotations.Test;importcom.testapi.until.ExcelUtils;importcom.testapi.until.Getcode;importcom.testapi.until.Makejson;importcom.testapi.until.ParseJson;import com.testapi.until.*;

@Listeners({ZTestReport.class})public classTestapi {

@DataProvider(name="DP1")public Object[][] createData() throwsException {

ExcelUtils excelUtils=newExcelUtils();

Object[][] m= ExcelUtils.getTableArray("casedata\\casedata.xlsx","Sheet1");returnm;

}

@Test(dataProvider="DP1")public voidf(String url,String path,String meth,String map,String jsonid,String qiwang) {

String param1=Makejson.makejson(map);

Getcode getcode=newGetcode();

url=url+path;

String resulst=getcode.getHttpResponse(param1, url,meth.toUpperCase());

String bnei=ParseJson.Json(resulst);

assertEquals(bnei,qiwang);

}

@BeforeTestpublic voidbeforeTest() {

}

@AfterTestpublic voidafterTest() {

}

}

这就是源码,这就是源码,

那么,你能让我看看你的Excel怎么写的吗,可以

13758f496a73d341e418268003bac0b9.png

目前支持的断言是断言code的字段,其实还可以丰富,接下来会优化这方面。

运行testng测试

6d0ed7116b0a5a97275f52fb5ae7187a.png

控制台输出

1e283c120bad64d4f3a886772623ffbc.png

最后的测试报告

d17be0a4e8b46802367629b8d7d5ada8.png

测试报告,要感谢飞总的ztest

开源代码   github  传送门   喜欢的可以star。

作者寄语:

前进的道路我们充满着迷茫,

前进的每一步我们都会有收获。

路在脚下,我们决定不了我们的出身,但是我们可以努力改变我们未来。

告别昨天失败的自己,努力拼搏今天,成就美好明天

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值