testng使用DataProvider+Excel实现DDT
生活随笔
收集整理的這篇文章主要介紹了
testng使用DataProvider+Excel实现DDT
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
DDT,即數(shù)據(jù)驅(qū)動(dòng)測(cè)試 Data Driver Test,我曾經(jīng)記錄了一篇關(guān)于python的DDT框架(Excel+DDT數(shù)據(jù)驅(qū)動(dòng)實(shí)例),那么java中的DDT是怎么樣的呢?在java中,可以用testng的DataProvider和Excel實(shí)現(xiàn)。
首先建一個(gè)文檔TestData.xlsx,里面內(nèi)容如下:
然后寫一個(gè)讀excel的公共類,代碼如下:
/*** @author Helen * @date 2018年6月5日 */ package common;import java.io.File; import java.io.FileInputStream; import java.io.IOException;import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /*** 描述:excel事件處理*/ public class MyExcel { /*讀取excel文件中的數(shù)據(jù),并生成數(shù)組*/@SuppressWarnings("deprecation")public Object[][] readExcel(String filePath,String sheetName) throws IOException {BaseData bdata = new BaseData();File file = new File(bdata.getFilePath(filePath));//獲取文件FileInputStream fileInputStream = new FileInputStream(file);//讀數(shù)據(jù) XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);XSSFSheet sheet = workbook.getSheet(sheetName);//讀取指定標(biāo)簽頁(yè)的數(shù)據(jù)int rowNum = sheet.getPhysicalNumberOfRows();//獲取行數(shù)(獲取的是物理行數(shù),也就是不包括那些空行(隔行)的情況)int columNum = sheet.getRow(0).getPhysicalNumberOfCells();//獲取列數(shù) Object[][] data = new Object[rowNum-1][columNum];//因?yàn)榈谝恍凶鳛樽侄蚊?#xff0c;不需要記錄,所以只有[rowNum-1]行for(int i=1;i<rowNum;i++) {//從第二行開始取值for (int h = 0; h < columNum; h++) {sheet.getRow(i).getCell(h).setCellType(Cell.CELL_TYPE_STRING);//先把類型設(shè)置為stringdata[i-1][h] = sheet.getRow(i).getCell(h).getStringCellValue();//填充數(shù)組 }}workbook.close();return data;}}excel的支持還需要在pom.xml加入如下內(nèi)容
<!--加入對(duì)excel的讀寫支持--><!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency>其次,寫一個(gè)數(shù)據(jù)源公共類(數(shù)據(jù)源也可以直接寫在測(cè)試類中,如果寫在測(cè)試類中就不用標(biāo)名數(shù)據(jù)源所在class),代碼如下:
/*** @author Helen * @date 2018年6月11日 */ package jinengxia_apiTest;import common.MyExcel; import java.io.IOException; import org.testng.annotations.DataProvider;/*** 描述:接口測(cè)試的數(shù)據(jù)源*/ public class myApiTestData {MyExcel myExcel = new MyExcel();@DataProvider(name = "loginData")public Object[][] getLoginData() throws IOException {return myExcel.readExcel("src/test/java/testFile/TestData.xlsx","login");} }
最后調(diào)用數(shù)據(jù)源,代碼如下:
/*** @author Helen * @date 2018年6月5日 */ package jinengxia_apiTest;import static org.testng.Assert.assertEquals;import java.io.IOException; import java.net.URI; import java.net.URISyntaxException;import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.testng.annotations.DataProvider; import org.testng.annotations.Test;import common.MyExcel; import net.sf.json.JSONObject;/*** 描述:用戶授權(quán)相關(guān)接口*/ public class UserAuthorization {CloseableHttpClient httpCilent = HttpClients.createDefault();HttpResponse httpResponse;MyExcel myExcel = new MyExcel();@Test(description = "用戶登錄",dataProvider="loginData",dataProviderClass=myApiTestData.class)public void login(String username,String password) throws URISyntaxException, ClientProtocolException, IOException {HttpUriRequest httpUriRequest = RequestBuilder.post().setUri(new URI("http://api.**.***.com/**")).addParameter("username", username).addParameter("password", password).build();HttpClientContext localContext = HttpClientContext.create();// 創(chuàng)建本地HTTP上下文,用于獲取請(qǐng)求頭cookies等內(nèi)容httpResponse = httpCilent.execute(httpUriRequest, localContext);String strResult = EntityUtils.toString(httpResponse.getEntity());// 獲得返回的結(jié)果JSONObject jsonObject = JSONObject.fromObject(strResult);assertEquals(jsonObject.get("message"), "登錄成功");}}執(zhí)行測(cè)試,結(jié)果如下:
?
轉(zhuǎn)載于:https://www.cnblogs.com/helenMemery/p/9167191.html
總結(jié)
以上是生活随笔為你收集整理的testng使用DataProvider+Excel实现DDT的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【BZOJ1924】【SDOI2010】
- 下一篇: MyBatis核心接口和类