白盒测试学习之TestNG的参数分离

发表于:2016-2-15 13:34

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:loleina    来源:51Testing软件测试网采编

  之前的测试用例直接嵌套在代码中,不便于维护和测试设计,应该单独把测试用例放在excel内,然后程序从中读取数据到相应的接口内即可。使用ava程序对Microsoft Office格式档案读和写的功能提供的POI库就可以完成读取数据的功能,而其中又主要使用的是HSSF类,该类 提供读写Microsoft Excel格式档案的功能。编写ParserExcel类,主要有三个函数:
  其中,readExcelContent方法主要用于读取excel的sheet下的相关数据,保存到数组Object中,并返回;showContent是自己测试的时候,打印出读取的数据所用,getCellFormatValue主要是用于将从excel中读取的数据进行格式转换,判断当前Cell的Type,然后进行各种处理;核心代码如下:
public Object[][] readExcelContent(String sheetName) {
sheet = wb.getSheet(sheetName);
int rowNum = sheet.getLastRowNum();
//getRow(int rownum) ,Returns the logical row (not physical) 0-based.
row = sheet.getRow(2);
//getPhysicalNumberOfCells:Gets the number of defined cells (NOT number of cells in the actual row!)
int colNum = row.getPhysicalNumberOfCells();
Object[][] ExcelInfo = new Object[rowNum-3][colNum-1];
for (int i = 0; i < rowNum-3; i++) {
row = sheet.getRow(i + 3);
for (int j = 0; j < colNum-1; j++) {
ExcelInfo[i][j] = getCellFormatValue(row.getCell(j+1)).trim();
}
}
return ExcelInfo;
}
  测试用例简单设计如下:
  然后修改测试程序代码:
public class testSourceCode {
static String tmp = "D:\\testcase.xls";
private static ParserExcel pe = new ParserExcel(tmp);
@Test(dataProvider="providerMethod")
public void testjudgeKindOfTriangle(String sa,String sb,String sc,String Expt) {
int a=Integer.parseInt(sa);
int b=Integer.parseInt(sb);
int c=Integer.parseInt(sc);
int tmp=Integer.parseInt(Expt);
int reslut =TriangleJudgeSourceCode.judgeKindOfTriangle(a,b,c);
Assert.assertEquals(reslut, tmp);
}
@DataProvider
public static Object[][] providerMethod(Method method){
String sheetName = method.getName();
System.out.println(sheetName);
return pe.readExcelContent(sheetName);
}
  运行该测试程序,测试结果如下:
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号