您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > TestNG
TestNG+HttpClient+Excel數(shù)據(jù)驅(qū)動(dòng)測(cè)試
作者:Emma_mmmm 發(fā)布時(shí)間:[ 2016/10/26 11:43:45 ] 推薦標(biāo)簽:單元測(cè)試工具 TestNG

  數(shù)據(jù)驅(qū)動(dòng)測(cè)試
  數(shù)據(jù)驅(qū)動(dòng)測(cè)試的核心是:測(cè)試數(shù)據(jù)與測(cè)試腳本分離,實(shí)現(xiàn)測(cè)試腳本參數(shù)化,提高測(cè)試腳本的可重用性。在自動(dòng)化功能測(cè)試中如果靈活使用數(shù)據(jù)源與測(cè)試腳本,便能輕松創(chuàng)建與運(yùn)行成百上千個(gè)測(cè)試用例。自動(dòng)化測(cè)試框架必須要有與電子表格、文本文件、數(shù)據(jù)庫(kù)集成的能力。
  TestNG
  TestNG是受JUnit和NUnit測(cè)試框架的啟發(fā)而設(shè)計(jì)的,但引其中入一些新的功能,使它更強(qiáng)大和更容易使用。(JUnit和NUnit我沒(méi)用過(guò),不做評(píng)價(jià))
  Apache POI
  Apache POI項(xiàng)目的使命是開(kāi)發(fā)和維護(hù)各種基于Office Open XML 標(biāo)準(zhǔn)(OOXML)和微軟文檔格式的Java api,使用Apache POI,可以方便讀寫微軟EXCEL、word、ppt等文檔。
  怎么做:
  第一步:工具準(zhǔn)備
  第二步:創(chuàng)建一個(gè)登錄接口測(cè)試用例
  第三步:用Excel創(chuàng)建測(cè)試數(shù)據(jù)
  第四步:使用Apache POI打開(kāi)與讀取Excel數(shù)據(jù)
  第五步:創(chuàng)建TestNg測(cè)試用例并使用Data Provider從Excel讀取數(shù)據(jù)
  第六步:根據(jù)測(cè)試用例名稱運(yùn)行測(cè)試
  第一步:工具準(zhǔn)備
  1.OS:win10 家庭中文版
  2.JDK:jdk-8u66-windows-x64.exe
  3.Eclipse:eclipse-inst-win64.exe
  4.TestNG插件
  5.HttpClient:httpcomponents-client-4.5.2-bin.zip
  6.Apache POI:poi-bin-3.14.zip
  第二步:創(chuàng)建一個(gè)查詢接口測(cè)試用例
  1.創(chuàng)建一個(gè)TestNG類:DataProviderTest
  2.引用注釋Dataprovider,這個(gè)方法會(huì)返回對(duì)象數(shù)組
  3.準(zhǔn)備兩條數(shù)據(jù),一條url,一條POST的json參數(shù)
  4.在@Test下寫一個(gè)查詢測(cè)試用例
package testData;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderTest {
@DataProvider(name="Authentication")
public static Object[][] credentials(){
String p1="http://192.168.1.55:8182/ebe/api/query_server/v1/query";
String pd="{'sqlText':'select * from ts','pageSize':'100','pageNum':'1'}";
return new Object[][] {{p1,pd}};
}
@Test(dataProvider="Authentication")
public void QuickStart(String p1,String pd) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(p1);
JSONObject jsonParam=new JSONObject(pd);
StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
System.out.println("Response content: " + EntityUtils.toString(entity2));
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);
} finally {
response2.close();
}
} finally {
httpclient.close();
}
}
}
  第三步:用Excel創(chuàng)建測(cè)試數(shù)據(jù)
  1.在該項(xiàng)目下新建一個(gè)testdata.xlxs文件,并寫入內(nèi)容


  
測(cè)試數(shù)據(jù).png

  第四步:使用Apache POI打開(kāi)與讀取Excel數(shù)據(jù)
  1.我們需要從Excel中讀取測(cè)試數(shù)據(jù),所以這里是用Apache POI對(duì)Excel進(jìn)行操作。
package testData;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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);
// Access the required test data sheet
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
int startRow = 1;
int startCol = 1;
int ci,cj;
int totalRows = ExcelWSheet.getLastRowNum();
System.out.println(totalRows);
// you can write a function as well to get Column count
int totalCols = 2;
System.out.println(totalCols);
tabArray=new String[totalRows][totalCols];
ci=0;
for (int i=startRow;i<=totalRows;i++, ci++) {
cj=0;
for (int j=startCol;j<=totalCols;j++, cj++){
tabArray[ci][cj]=getCellData(i,j);
System.out.println(tabArray[ci][cj]);
}
}
}
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);
}
}
}

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd