In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "Test-driven Technology Series: how to read data from excel". In daily operation, I believe many people have doubts about how to read data from excel. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Test-driven Technology Series: how to read data from excel"! Next, please follow the editor to study!
General form of Parameterization in Test Framework
The form of parameterization in Junit4 and TestNG frameworks is almost the same.
Junit4 defines parameterized data as follows:
Public static Collection prepareData () {Object [] [] object= {# # * * $$} 1je 2jue 3}, {0reco 2jue 2}, {0rem 3je 3}}; return Arrays.asList (object);}
TestNG defines parameterized data as follows:
@ DataProvider public Object [] [] dp1 () {return new Object [] [] {new Object [] {1jue 1,0}, new Object [] {2,1jue 1}, new Object [] {2,1Magne2},};}
The realization idea of reading test-driven data from excel
In the previous article, I explained in detail the application of arrays in java and how to use api to manipulate excel files. Next, I will explain to you the core code and implementation ideas of reading excel in the testing framework for interface test parameterization.
1. First of all, we see that the parameterized return value of the test data is Object [] [] object, then we need to write a method to read excel. The return value of this method is Object [] [], and the method can be defined as: Object [] [] readExcel (String paired file int p_sheetindex).
The name of reading cell in 2.excel is confirmed by rows and columns, and different types of cell use different api when reading values. We write a worthwhile method to read Cell. The code is as follows:
Public static Object getCellVaule {Object value=null; Row row = p_sheet.getRow (p_rowIndex); Cell cell = row.getCell (p_cellIndex); if (cell.getCellType () = = CellType.NUMERIC) / / determines that it is a numeric type {value= cell.getNumericCellValue () } else if (cell.getCellType () = = CellType.STRING) / / judge to be String type {value=cell.getStringCellValue ();} else if (cell.getCellType () = = CellType.BOOLEAN) / / judge to be BOOLEAN type {value=cell.getStringCellValue ();} else {value=cell.toString () / / other types are converted into String types} return value;}
3. Combined with the getCellVaule method, we write a method to read the complete excel value, and the return value is Object [] []. The code is as follows:
Public static Object [] [] readExcel (String paired filenint p_sheetindex) {Workbook workbook=null; Object [] [] data=null; try {workbook= new XSSFWorkbook (new FileInputStream (p_file)); Sheet sheet = workbook.getSheetAt (p_sheetindex); / / get the first worksheet .0 represents the first sheet page int iRowNum=sheet.getPhysicalNumberOfRows () / / get the number of rows in the worksheet int iColumnNum= sheet.getRow (0). GetPhysicalNumberOfCells (); / get the number of columns in the worksheet data=new Object [iRowNum] [iColumnNum]; / / create a two-dimensional array to hold the data for in the cell through the number of rows.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.