亚洲好骚综合-亚洲黄色录像-亚洲黄色网址-亚洲黄色网址大全-99久久99久久-99久久99久久精品国产

您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源單元測(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

  第五步:創(chuàng)建TestNg測(cè)試用例并使用Data Provider從Excel讀取數(shù)據(jù)
  1.創(chuàng)建TestNG類:DataProviderWithExcel,從excel中導(dǎo)入數(shù)據(jù),并執(zhí)行query操作
package testData;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.JSONObject;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderWithExcel {
@DataProvider
public Object[][] Authentication() throws Exception{
Object[][] testObjArray=ExcelUtils.getTableArray("F://Users//Tangxi//workspace//apiTest//src//testData//testData.xlsx", "Sheet1");
return (testObjArray);
}
@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();
}
}
}
  第六步:根據(jù)測(cè)試用例名稱運(yùn)行測(cè)試
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 ExcelUtils2 {
private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell Cell;
private static XSSFRow Row;
//This method is to set the File path and to open the Excel file, Pass Excel Path and Sheetname as Arguments to this method
public static void setExcelFile(String Path,String SheetName) throws Exception {
try {
// Open the Excel file
FileInputStream ExcelFile = new FileInputStream(Path);
// Access the required test data sheet
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
} catch (Exception e){
throw (e);
}
}
public static Object[][] getTableArray(String FilePath, String SheetName, int iTestCaseRow)    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 startCol = 1;
int ci=0,cj=0;
int totalRows = 1;
int totalCols = 2;
tabArray=new String[totalRows][totalCols];
for (int j=startCol;j<=totalCols;j++, cj++)
{
tabArray[ci][cj]=getCellData(iTestCaseRow,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);
}
//This method is to read the test data from the Excel cell, in this we are passing parameters as Row num and Col num
public static String getCellData(int RowNum, int ColNum) throws Exception{
try{
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
String CellData = Cell.getStringCellValue();
return CellData;
}catch (Exception e){
return"";
}
}
public static String getTestCaseName(String sTestCase)throws Exception{
String value = sTestCase;
try{
int posi = value.indexOf("@");
value = value.substring(0, posi);
posi = value.lastIndexOf(".");
value = value.substring(posi + 1);
System.out.println(value);
return value;
}catch (Exception e){
throw (e);
}
}
public static int getRowContains(String sTestCaseName, int colNum) throws Exception{
int i;
try {
int rowCount = ExcelUtils2.getRowUsed();
for ( i=0 ; i<rowCount; i++){
if  (ExcelUtils2.getCellData(i,colNum).equalsIgnoreCase(sTestCaseName)){
break;
}
}
return i;
}catch (Exception e){
throw(e);
}
}
public static int getRowUsed() throws Exception {
try{
int RowCount = ExcelWSheet.getLastRowNum();
return RowCount;
}catch (Exception e){
System.out.println(e.getMessage());
throw (e);
}
}
}
  后的測(cè)試用例
  1.獲取測(cè)試用例名稱
  2.根據(jù)測(cè)試用例名稱獲取對(duì)應(yīng)的行號(hào)
  3.從對(duì)應(yīng)的行中獲取測(cè)試數(shù)據(jù)
package testData;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.JSONObject;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import testData.ExcelUtils2;
public class DataProviderWithExcel_002 {
private String sTestCaseName;
private int iTestCaseRow;
@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();
}
}
@DataProvider
public Object[][] Authentication() throws Exception{
// Setting up the Test Data Excel file
ExcelUtils2.setExcelFile("F://Users//Tangxi//workspace//apiTest//src//testData//testData.xlsx","Sheet1");
sTestCaseName = this.toString();
// From above method we get long test case name including package and class name etc.
// The below method will refine your test case name, exactly the name use have used
sTestCaseName = ExcelUtils2.getTestCaseName(this.toString());
System.out.println(sTestCaseName);
// Fetching the Test Case row number from the Test Data Sheet
// Getting the Test Case name to get the TestCase row from the Test Data Excel sheet
iTestCaseRow = ExcelUtils2.getRowContains(sTestCaseName,0);
System.out.println(iTestCaseRow);
Object[][] testObjArray = ExcelUtils2.getTableArray("F://Users//Tangxi//workspace//apiTest//src//testData//testData.xlsx","Sheet1",iTestCaseRow);
return (testObjArray);
}
}

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd
主站蜘蛛池模板: 性欧美.video.free | 午夜精品国产 | 97精品伊人久久久大香线焦 | 中国产一级毛片 | 成人免费视频在线观看 | 久久成人激情视频 | 九九精品视频一区在线 | 欧美疯狂xxxx乱大交视频 | 玖玖视频精品 | 涩涩爱在线观看 | 黄色的视频免费看 | 亚洲成人激情在线 | 狠狠欧美 | 波多野结衣中文字幕在线播放 | 一级一级一片免费高清 | 国产日本久久久久久久久婷婷 | 宅男在线 午夜影院 | 午夜视频网站在线观看 | 欧美一级二级三级视频 | 色爱综合区五月小说 | 在线a人片免费观看高清 | 亚洲人成绝费网站色ww | 一级a俄罗斯毛片免费 | 欧美激情免费a视频 | 99国产超薄丝袜足j在线观看 | 午夜影视福利 | 亚洲人成人77777网站不卡 | 500第一导航亚洲精品导航 | 精品成人在线视频 | 国产成人免费永久播放视频平台 | 五月天婷婷在线视频 | 欧美精品国产一区二区 | 国产制服在线 | 国产一区二区三区不卡观 | 色天使色婷婷在线影院亚洲 | 黑人videovideosex| 综合网天天| 黄大色大片免费久久 | 日本护士色xxxxx视频 | 天天操夜夜操免费视频 | 日本一区二区在线 |