Java接口自動化測試(四) — 使用Excel做數據驅動


一、說明

  1. 數據驅動測試的核心:是測試數據與測試腳本分離,實現測試腳本的參數化,例如:在使用工具測試時,常常會使用到參數化設置;
  2. 使用數據驅動測試方便后期維護,提高腳本的可重用性;
  3. 做數據驅動的方式有多種例如:Excel、CSV、MySQL等

二、使用Excel實現數據驅動

1、在pom文件添加POI依賴

       <!--操作Excel數據-->
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.17</version>
        </dependency>

2、新建一個Class命名為ExcelUtils

package com.test.excel; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author lh * @date 2020/7/3 * @description */
public class ExcelUtils { /** * 讀取Excel數據 * @return List * @throws Exception */
    public static List<Map<String ,Object>> readExcle() throws Exception{ //用流的方式讀取Excel文件
        FileInputStream fis = new FileInputStream("C:\\Users\\Think\\Desktop\\test.xlsx"); //獲取Excel工作簿
        XSSFWorkbook xf = new XSSFWorkbook(fis); //獲取第一個sheet
        XSSFSheet sheet = xf.getSheetAt(0); //獲取第一行
        int firstRow = sheet.getFirstRowNum(); //獲取最后一行
        int lastRow = sheet.getLastRowNum(); // System.out.println(lastRow); //用於map設置key值,自定義
        String columns[] = {"username", "password"}; List< Map<String ,Object>> list = new ArrayList<>(); for (int i = 0;i<=lastRow;i++){ XSSFRow row = sheet.getRow(i); if (row!=null){ //獲取第一行,第一列
                int firstcell = row.getFirstCellNum(); //獲取最后一行,最后一列
                int lastcell = row.getLastCellNum(); Map<String ,Object> map = new HashMap<>(); for (int j = 0;j<columns.length;j++){ XSSFCell cell = row.getCell(j); if (cell == null){ continue; } //封裝成map
 map.put(columns[j], getCellFormatValue(cell)); } //System.out.println(map); //將map放入List
 list.add(map); } fis.close(); xf.close(); } return list; } //格式化數值類型
    public static Object getCellFormatValue(Cell cell){ Object cellValue = null; /** * getCellTypeEnum()方法是枚舉類型,用於判斷單元格值類型,有以下五種格式: * _NONE(-1), * NUMERIC(0), * STRING(1), * FORMULA(2), * BLANK(3), * BOOLEAN(4), * ERROR(5); */
        switch(cell.getCellTypeEnum()){ case STRING: cellValue = cell.getStringCellValue(); break; case NUMERIC: /**如果是數字類型轉換成數字類型,但是初始化數字加的.0,因此可以轉換成int類型去掉.0 * cell.getNumericCellValue() */ cellValue = new Double(cell.getNumericCellValue()).intValue(); break; case BOOLEAN: break; case FORMULA: break; } return cellValue; } //測試
    public  static void main(String[] arg) throws Exception{ ExcelUtils excelTest = new ExcelUtils(); excelTest.readExcle(); } }

 

三、在測試用例中使用

  1. 在testng中使用需要新創建一個方法getDataMethod(),返回Object[][]類型
  2. 使用注解@DataProvider,為測試方法提供數據
  3. 在測試方法上使用@Test(dataProvider = "getDataMethod")接收數據
  4. 使用數據驅動有一個好處,測試方法可以寫一個,根據參數頻繁調用一個方法(和上一篇對比https://www.cnblogs.com/liho/p/13225137.html
package com.test.httpclient; import com.alibaba.fastjson.JSONObject; import com.test.excel.ExcelUtils; import org.testng.Assert; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.util.List; import java.util.Map; /** * @author lh * @date 2020/6/8 * @description */ @Test public class LoginCase { HttpClientPost httpClientPost = new HttpClientPost(); @DataProvider public Object[][] getDataMethod() throws Exception{
     //讀取Excel中的數據 List
<Map<String,Object>> result = ExcelUtils.readExcle(); Object[][] params = new Object[result.size()][]; for (int i = 0; i < result.size(); i++) { params[i] = new Object[]{result.get(i)}; } return params; } @BeforeTest public void setup(){ System.out.println("用例執行前執行"); }    //一個測試方法 @Test(dataProvider = "getDataMethod") public void test1(Map<String, Object> param) throws Exception { JSONObject result = httpClientPost.LoginPost(param); Assert.assertEquals(result.get("code"),200); } @AfterTest public void teardown(){ System.out.println("用例執行完后執行"); } }

四、查看運行結果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM