解析Excel,通过Excel驱动测试数据参数化
来源:互联网 发布:三国战纪神剑进阶数据 编辑:程序博客网 时间:2024/05/21 18:49
1.解析Excel:
public class ExcelDataProvider implements Iterator<Object[]>{ private Workbook book = null; private Sheet sheet = null; private int rowNum = 0; private int curPhysicalRowNo = 0; private int columnNum = 0; private String[] columnnName; public ExcelDataProvider(String classname, String methodname)throws BiffException, IOException { File directory = new File("."); this.book = Workbook.getWorkbook(new File(directory.getCanonicalPath()+ "\\filepath\\" + classname.replaceAll("\\.", "/") + ".xls")); System.out.println(directory.getCanonicalPath() + "\\filepath\\"+ classname.replaceAll("\\.", "/") + ".xls"); this.sheet = book.getSheet(methodname); this.rowNum = sheet.getRows(); //System.out.println(rowNum); Cell[] c = sheet.getRow(0); /* for(Cell c1:c){ System.out.println(c1.getContents()); }*/ this.columnNum = c.length; //System.out.println(columnNum); columnnName = new String[c.length]; for (int i = 0; i < c.length; i++) { columnnName[i] = c[i].getContents().toString().replace("\n", ""); } this.curPhysicalRowNo++; } @Overridepublic boolean hasNext() { if (this.rowNum == 0 || this.curPhysicalRowNo >= this.rowNum) { try { book.close(); } catch (Exception e) { e.printStackTrace(); } return false; } else return true;}@Overridepublic Object[] next() {Map<String, ArrayList<String>> s = new HashMap<String, ArrayList<String>>(); int RangeRow = 1; boolean ThisIsRange = false; jxl.Range[] ranges = sheet.getMergedCells(); //首先要确定第一列占了几行,要区分 Range 还是 Row for (jxl.Range space : ranges) { if (space.getTopLeft().getColumn() == 0 && space.getBottomRight().getColumn() == 0 && space.getTopLeft().getRow() == this.curPhysicalRowNo) { RangeRow = space.getBottomRight().getRow() - space.getTopLeft().getRow() + 1; ThisIsRange = true; break; } } for (int i = 0; i < this.columnNum; i++) { ArrayList<String> temp = new ArrayList<String>(); if (ThisIsRange) { if (i == 0) { temp.add(sheet.getRow(this.curPhysicalRowNo)[i].getContents().toString()); } else { for (int j = 0; j < RangeRow; j++) { temp.add(sheet.getRow(this.curPhysicalRowNo + j)[i].getContents().toString()); } } } else { temp.add(sheet.getRow(this.curPhysicalRowNo)[i].getContents().toString()); } s.put(this.columnnName[i], temp); } Object r[] = new Object[1]; r[0] = s; this.curPhysicalRowNo = this.curPhysicalRowNo + RangeRow; return r;}@Overridepublic void remove() { throw new UnsupportedOperationException("remove unsupported.");}}2.测试方法中调用dataProvider,将测试数据参数化:
方式一:直接定义数据
@Test(dataProvider = "dp") public void testA(Map<String, ArrayList<String>> data) { System.out.println(data.toString()); System.out.println("------------------"); System.out.print("字段A:" + data.get("字段A")); System.out.println(""); System.out.print("字段B:" + data.get("字段B")); System.out.println(""); System.out.print("字段C:" + data.get("字段C")); System.out.println(""); System.out.print("字段D:" + data.get("字段D")); System.out.println(""); System.out.println("*******************"); } @DataProvider(name="dp") public Iterator<Object[]> dataFortestMethod(Method method)throws IOException, BiffException { return new ExcelDataProvider(this.getClass().getName(), method.getName()); }方式二:
通过数据持久化层,将Excel中的数据值赋予给变量
数据持久化层:
public class Enterprise_reporting_demo {public String standardId;public String evaluateDate;public String evaluateMan;public String evaluateJoiner;public Enterprise_reporting_demo(String standardId,String evaluateDate,String evaluateMan,String evaluateJoiner){this.standardId=standardId;this.evaluateDate=evaluateDate;this.evaluateMan=evaluateMan;this.evaluateJoiner=evaluateJoiner;}}测试方法中调用:
@Test(dataProvider="dp")public void test5(Map<String,ArrayList<String>>data){Enterprise_reporting_demo enterprise_reporting_demo=new Enterprise_reporting_demo(data.get("standardId").get(0), data.get("evaluateDate").get(0), data.get("evaluateMan").get(0), data.get("evaluateJoiner").get(0));enterprise_reporting.del_Enterprise_reporting(enterprise_reporting_demo);Assert.assertFalse(enterprise_reporting.del);}@DataProvider(name="dp") public Iterator<Object[]> dataFortestMethod(Method method)throws IOException, BiffException { return new ExcelDataProvider(this.getClass().getName(), method.getName()); }注意:Excel文件的命名应该和测试方法的命名一致,否则会提示:找不到文件
- 解析Excel,通过Excel驱动测试数据参数化
- mfc 通过excel驱动读取excel文件
- 将 Excel 用于测试数据
- nodejs通过tb-excel自定义解析Excel
- 通过poi解析Excel示例
- 通过poi解析excel文件
- QTP测试数据管理-Excel+Dictionary
- qtp参数化-通过excel导入到datatable中
- java通过jxl解析Excel文件
- Java 通过Poi解析数据到EXCEL
- IOS 通过Objective-C读取、解析Excel
- 通过python 使用xlrd解析Excel
- java通过POI解析Excel表格内容
- 测试程序通过excel读取测试数据达到数据与程序的分离
- Excel解析
- 解析excel
- 解析excel
- Excel解析
- JVM调优(九)-新一代的垃圾回收算法
- OGG DDL 双向同步
- boost------asio库的使用2(Boost程序库完全开发指南)读书笔记
- 在Django中使用最新版tinymce
- IOSOpenDev~抛弃数据线,用无线路IFILE上传文件
- 解析Excel,通过Excel驱动测试数据参数化
- JVM调优(十)-调优方法
- myEclipse 安装ibatis插件
- ARM CPSR+SPSR
- 区分Activity的四种加载模式
- flex4 的时间轴动画
- git 创建项目
- JVM调优(十一)-反思
- SQL Server中的STUFF函数的使用 .