java 接口測試,使用excel做數據驅動(二)


 承接上篇

 改變我們的測試驅動方式,靈活設置測試用例。

數據驅動測試

數據驅動測試的核心是:

測試數據與測試腳本分離,實現測試腳本參數化,

提高測試腳本的可重用性。在自動化功能測試中如果靈活使用數據源與測試腳本,

便能輕松創建與運行成百上千個測試用例。自動化測試框架必須要有與電子表格、文本文件、數據庫集成的能力。

 

首先小伙伴們就會問,你要先解析Excel吧,那你肯定得給我上代碼,是的,必須的上代碼。

package com.testapi.until;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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;
public class ExcelUtils {
    private static XSSFSheet ExcelWSheet;
    private static XSSFWorkbook ExcelWBook;
    private static XSSFCell Cell;
    private static XSSFRow Row;
    public static Object[][] getTableArray(String FilePath, String SheetName) throws Exception {   
       String[][]tabArray = null;
       try {
           FileInputStream ExcelFile = new FileInputStream(FilePath);
           ExcelWBook = new XSSFWorkbook(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) throws Exception {
        try{
            Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
            int dataType = Cell.getCellType();
            if  (dataType == 3) {
                return "";
            }
            else{
                String CellData = Cell.getStringCellValue();
                return CellData;
            }
        }
            catch (Exception e){
//            System.out.println(e.getMessage());
            throw (e);
            }
        }
    public static void main(String[] args) throws Exception {
        ExcelUtils excelUtils=new ExcelUtils();
        Object[][] m = excelUtils.getTableArray("C:\\Users\\Administrator\\eclipse-workspace\\ApiTest\\casedata\\casedata.xlsx","Sheet1");
 
    }
    }

這就是我們的代碼,那么小伙伴迫不及待了,你快來告訴我,

你的測試用例怎么組織的,好的 ,熱騰騰的鈣素你,我給你的就是代碼,就是源碼。源碼如下

 

package com.testapi.casev;
import static org.testng.Assert.assertEquals;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import com.testapi.until.ExcelUtils;
import com.testapi.until.Getcode;
import com.testapi.until.Makejson;
import com.testapi.until.ParseJson;
import com.testapi.until.*;
@Listeners({ZTestReport.class})
public class Testapi {
    @DataProvider(name="DP1")
    public Object[][] createData() throws Exception {
        ExcelUtils excelUtils=new ExcelUtils();
        Object[][] m = ExcelUtils.getTableArray("casedata\\casedata.xlsx","Sheet1");
        return m;
    }
  @Test(dataProvider="DP1")
  public void f(String url,String path,String meth,String map,String jsonid,String qiwang) {
    String param1 = Makejson.makejson(map);
    Getcode getcode=new Getcode();
    url=url+path;
    String resulst=getcode.getHttpResponse(param1, url,meth.toUpperCase());
    String bnei=ParseJson.Json(resulst);
    assertEquals(bnei,qiwang);
  }
  @BeforeTest
  public void beforeTest() {
  }
  @AfterTest
  public void afterTest() {
  }

}

 

 

這就是源碼,這就是源碼,

 

那么,你能讓我看看你的Excel怎么寫的嗎,可以

 

目前支持的斷言是斷言code的字段,其實還可以豐富,接下來會優化這方面。

運行testng測試

 

控制台輸出

 

 

最后的測試報告

 

 

 

 

 

 測試報告,要感謝飛總的ztest

開源代碼    github  傳送門   喜歡的可以star。 

 

 

作者寄語:

前進的道路我們充滿着迷茫,

前進的每一步我們都會有收獲。

路在腳下,我們決定不了我們的出身,但是我們可以努力改變我們未來。

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


免責聲明!

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



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