基于httpclient的数据驱动接口自动化实例

为什么要做接口自动化?

可参阅:http://www.sohu.com/a/152383408_99919686

实例机构如下:

基于httpclient的数据驱动接口自动化实例_第1张图片

架构使用前提所需jar包:

1.httpclient.jar

2.Apache POI:poi-bin-3.14.zip

具体实现

1.创建一个Excel 将用例的名称、接口的URL、接口报文添加到Excel中

基于httpclient的数据驱动接口自动化实例_第2张图片

2.创建Java工程(maven 也可以,本教程先以java工程为例)

创建ExcelReader类

public class ExcelReader {

	private static XSSFSheet ExcelWSheet;
	private static XSSFWorkbook ExcelWBook;
	private static XSSFCell Cell;
	private static XSSFRow Row;
	public static String[][] getExpectationData(String file, String sheetName) {
		try {
			FileInputStream ExcelFile = new FileInputStream(file);//获取Excel文件
			ExcelWBook = new XSSFWorkbook(ExcelFile);
			// 得到工作表名称
			ExcelWSheet = ExcelWBook.getSheet(sheetName);
			// 得到总行数
			int rowNum = ExcelWSheet.getLastRowNum();
			List results = new ArrayList();
			for (int i = 1; i <= rowNum; i++) {
				// 当前行
				XSSFRow row = ExcelWSheet.getRow(i);
				int colNum = row.getLastCellNum();
				String[] data = new String[colNum];
				// 当前行所有列
				for (int j = 0; j < colNum; j++) {
					try {
						data[j] = getCellValue(row.getCell(j));
					} catch (NullPointerException e) { // 如果单元格为空的时候,则用这个来处理
						data[j] = "";
					}
				}
				// 把data[]数组的数据存在list<[]>中
				results.add(data);
			}

			String[][] returnArray = new String[results.size()][rowNum];
			for (int i = 0; i < returnArray.length; i++) {
				returnArray[i] = (String[]) results.get(i);
			}
			return returnArray;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 对Excel的各个单元格的格式进行判断并转换
	 */
	public static String getCellValue(XSSFCell xssfCell) {
		String cellValue = "";
		DecimalFormat df = new DecimalFormat("#");
		switch (xssfCell.getCellType()) {
		case HSSFCell.CELL_TYPE_STRING:
			cellValue = xssfCell.getRichStringCellValue().getString().trim();
			break;
		case HSSFCell.CELL_TYPE_NUMERIC:
			cellValue = df.format(xssfCell.getNumericCellValue()).toString();
			break;
		case HSSFCell.CELL_TYPE_BOOLEAN:
			cellValue = String.valueOf(xssfCell.getBooleanCellValue()).trim();
			break;
		case HSSFCell.CELL_TYPE_FORMULA:
			cellValue = xssfCell.getCellFormula();
			break;
		default:
			cellValue = "";
		}
		return cellValue;
	}
}

3.创建testng测试类

public class Demo3 {


	@Test(dataProvider = "interfaceTest")
	public void getInterface(String case_1, String url, String assert_1) throws IOException {

		CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建默认的httpClient实例
		
		HttpGet httpget = new HttpGet(url); // 创建GET方法的实例,并传入待连接的地址
		System.out.println(url);
		Reporter.log("测试请求接口URL是:" + url);
		// 设置请求头文件
		httpget.addHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8");
		
		CloseableHttpResponse response = httpclient.execute(httpget);
		
		HttpEntity entity = response.getEntity(); // 读response,获取响应实体

		String getResult = EntityUtils.toString(entity, "UTF-8");// 将response转化为String类型 utf-8编码
		Reporter.log("接口返回的报文是:" + getResult);

		if (getResult != null) {
			System.out.println(case_1);
			System.out.println("接口响应:" + getResult + "\n"); // 打印响应内容
			EntityUtils.consume(entity);
		}

		httpclient.close(); // 关闭连接
		Assert.assertEquals(assert_1, getResult); // 断言
		Reporter.log(case_1); // 输出报告
	}

	@DataProvider(name = "interfaceTest") 
	public static Object[][] words() throws IOException {
		// 测试数据准备
		String file = "";//这里写入你excel的路径
		Object[][] records;
		records = ReadExcel.getExpectationData(file, "");//这里要写入你Excel中的sheetname
		return records;
	}
}
本文思路感谢:https://blog.csdn.net/yan1234abcd/article/details/62038230


你可能感兴趣的:(接口自动化)