DDT,即数据驱动测试 Data Driver Test,我曾经记录了一篇关于python的DDT框架(Excel+DDT数据驱动实例),那么java中的DDT是怎么样的呢?在java中,可以用testng的DataProvider和Excel实现。
首先建一个文档TestData.xlsx,里面内容如下:
然后写一个读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文件中的数据,并生成数组*/@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);//读数据 XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);XSSFSheet sheet = workbook.getSheet(sheetName);//读取指定标签页的数据int rowNum = sheet.getPhysicalNumberOfRows();//获取行数(获取的是物理行数,也就是不包括那些空行(隔行)的情况)int columNum = sheet.getRow(0).getPhysicalNumberOfCells();//获取列数 Object[][] data = new Object[rowNum-1][columNum];//因为第一行作为字段名,不需要记录,所以只有[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);//先把类型设置为stringdata[i-1][h] = sheet.getRow(i).getCell(h).getStringCellValue();//填充数组 }}workbook.close();return data;}}
excel的支持还需要在pom.xml加入如下内容
<!--加入对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>
其次,写一个数据源公共类(数据源也可以直接写在测试类中,如果写在测试类中就不用标名数据源所在class),代码如下:
/*** @author Helen * @date 2018年6月11日 */ package jinengxia_apiTest;import common.MyExcel; import java.io.IOException; import org.testng.annotations.DataProvider;/*** 描述:接口测试的数据源*/ 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");} }
最后调用数据源,代码如下:
/*** @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;/*** 描述:用户授权相关接口*/ 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();// 创建本地HTTP上下文,用于获取请求头cookies等内容httpResponse = httpCilent.execute(httpUriRequest, localContext);String strResult = EntityUtils.toString(httpResponse.getEntity());// 获得返回的结果JSONObject jsonObject = JSONObject.fromObject(strResult);assertEquals(jsonObject.get("message"), "登录成功");}}
执行测试,结果如下: