Excel+Httpclient,数据驱动接口自动化测试
来源:互联网 发布:蛀牙漱口水推荐 知乎 编辑:程序博客网 时间:2024/05/21 21:44
N个B端产品,一个产品有N个http接口、一个http接口又有N个入参,每个入参都要作非法性测试,一遍遍修改报文、再进行post,重复劳动可想而知。尝试写了一个简陋的api自动化框架,具体如下。还待持续完善。
一、框架设计
1、每一个http接口定义成一个类,以接口号为类名,属性为该接口的各个入参,用构造方法实现数据初始化。
2、制作一个excel模板1作为基础模板,模板1包含TestCase No.、TestCase Name、req请求报文、check检查点、res返回报文、status用例执行状态这几个必须的字段。
3、调用getNewMould方法,传入接口号,生成适用于该接口的excel模板2。模板2相比模板1,增加了字段“#入参1”、”#入参2”……
4、维护模板2,编写用例,填入测试数据、检查点。用例执行时,若单元格为空,对应入参使用初始数据;若单元格值为”*NULL”,对应入参为空值,以作入参必输性测试;若单元格有值且不为“*NULL”,则以单元格值覆盖初始值。
5、调用oneInterface方法,逐条执行用例post http请求,将请求报文记到req字段下,响应报文记到res字段下,判断响应报文中是否还有check字段,然后将用例置成fail、pass、warn3种状态。
二、代码示例
1、以接口号为名定义一个类
package util;public class 接口号{ String 入参1; String 入参2; ...... String 入参N; // 构造方法,初始化对象 接口号() { this.入参1 = ......; this.入参2 = ......; ...... this.入参N=......; } }
2、getNewMould方法
public static void getNewMould(String mouldpath, String sheetname, String dstpath, String clzname) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { FileInputStream ExcelFile = new FileInputStream(mouldpath); ExcelWBook = new XSSFWorkbook(ExcelFile); ExcelWSheet = ExcelWBook.getSheet(sheetname); XSSFFont font1 = ExcelWBook.createFont(); font1.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);// 加粗显示 font1.setFontName("宋体"); XSSFCellStyle style1 = ExcelWBook.createCellStyle(); style1.setFont(font1); // style1.setFillBackgroundColor(HSSFColor.LIGHT_BLUE.index); // 获取最后的列号 int lastcellnum = ExcelWSheet.getRow(0).getLastCellNum() - 1;// getLastCellNum获取列数 // 设入接口参数 Class<?> clz = Class.forName("util." + clzname); Object obj = clz.newInstance(); Field[] fields = obj.getClass().getDeclaredFields(); for (int ci = lastcellnum + 1, i = 0; i <= fields.length; ci++, i++) { if (i < fields.length) ExcelWSheet.getRow(0).createCell(ci) .setCellValue("#" + fields[i].getName()); else ExcelWSheet.getRow(0).createCell(ci).setCellValue("#sign"); Cell = ExcelWSheet.getRow(0).getCell(ci); Cell.setCellStyle(style1); } FileOutputStream fout = new FileOutputStream(dstpath); ExcelWBook.write(fout); fout.flush(); fout.close(); }
3、oneInterface方法
public static void oneInterface(String ExlPath, String Sheetname, int StartRowId, String CerPath, String pwd, String clzname, String url) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { // 检查excel、sheet是否存在 try { FileInputStream ExcelFile = new FileInputStream(ExlPath); ExcelWBook = new XSSFWorkbook(ExcelFile); ExcelWSheet = ExcelWBook.getSheet(Sheetname); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("FileNotFoundException: fail to read excel"); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("IOException: fail to read excel"); e.printStackTrace(); } // 入参校验 if (StartRowId < 1) throw new IllegalArgumentException("Illegal Arguments"); // 获取最后的行、列号 int lastrownum = ExcelWSheet.getLastRowNum();// getLastRowNum获取最后行号 int lastcellnum = ExcelWSheet.getRow(StartRowId - 1).getLastCellNum() - 1;// getLastCellNum获取列数 // 获取req、res、check、status字段列号 int reqci = 0, resci = 0, checkci = 0, statusci = 0; for (int k = 0; k <= lastcellnum; k++) { if (ExcelWSheet.getRow(StartRowId - 1).getCell(k) == null) { continue; } else { switch (ExcelWSheet.getRow(StartRowId - 1).getCell(k) .getStringCellValue()) { case "req": reqci = k; break; case "res": resci = k; break; case "check": checkci = k; break; case "status": statusci = k; break; } } } boolean ifpara = false; String value = ""; for (int ri = StartRowId; ri <= lastrownum; ri++) { // WHPU0001 a = new WHPU0001(); Object obj = Class.forName("util." + clzname).newInstance(); LinkedHashMap<String, String> map = ClassCommonUtil .getPropertiesMap(obj); for (int ci = 0; ci <= lastcellnum; ci++) { // 单元格内容转成字符串 String para = String.valueOf((ExcelWSheet .getRow(StartRowId - 1).getCell(ci))); ifpara = para.startsWith("#");// 判断是否为入参 if (null == ExcelWSheet.getRow(ri).getCell(ci) || ifpara == false) { continue; } else { if (ExcelWSheet.getRow(ri).getCell(ci).getStringCellValue() .equals("$NULL") == false) { value = ExcelWSheet.getRow(ri).getCell(ci) .getStringCellValue(); } else value = ""; String key = para.substring(1); map.put(key, value); } } JSONObject json = null; try { json = SmkUtil.map2Json(map, CerPath, pwd); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("发送" + json); String res = SmkUtil.httpPost(url, json, false); System.out.println("返回" + res); XSSFCell rescell = ExcelWSheet.getRow(ri).createCell(resci); rescell.setCellType(XSSFCell.CELL_TYPE_STRING); // 设入响应 rescell.setCellValue(res); // 设入请求 ExcelWSheet.getRow(ri).createCell(reqci) .setCellValue(String.valueOf(json)); // 设入状态 String checkstr = ""; if (ExcelWSheet.getRow(ri).getCell(checkci) != null) checkstr = ExcelWSheet.getRow(ri).getCell(checkci) .getStringCellValue(); if (res == null || res.startsWith("WARN:") || checkstr == "") { ExcelWSheet.getRow(ri).createCell(statusci) .setCellValue("WARN"); } else if (res.contains(checkstr)) { ExcelWSheet.getRow(ri).createCell(statusci) .setCellValue("PASS"); } else ExcelWSheet.getRow(ri).createCell(statusci) .setCellValue("FAIL"); } FileOutputStream fout = new FileOutputStream(ExlPath); ExcelWBook.write(fout); fout.flush(); fout.close(); }
0 0
- Excel+Httpclient,数据驱动接口自动化测试
- excel数据驱动接口测试框架
- WEB接口测试之Jmeter接口测试自动化 (三)(数据驱动测试) 接口测试与数据驱动
- 自动化测试过程中如何使用excel数据驱动(下)
- 自动化测试过程中如何使用excel数据驱动(上)
- 基于数据驱动的接口自动化测试解决方案
- WEB接口测试之Jmeter接口测试自动化 (三)(数据驱动测试)
- 基于httpclient和mysql的接口功能自动化测试框架
- 【JMeter 菜鸟实操之四】接口自动化测试实施(数据驱动)
- 【自动化测试】HttpClient 模拟用户登录与请求接口完成自动化测试
- 关键字+数据驱动自动化测试框架
- 自动化测试常用的数据驱动方式
- 自动化测试---被玩坏的数据驱动
- 自动化测试之 数据驱动 和 关键字驱动
- seleniumWebDriver自动化测试框架_01TestNG数据驱动测试
- 【接口测试】接口自动化测试
- testNG+httpclient接口测试
- 接口测试 - httpClient
- 精通java之IO流详解
- Action的生命周期及调用非execute方法
- H5touch事件监听
- Android 6.0权限申请
- 大规模优化算法 - LBFGS算法
- Excel+Httpclient,数据驱动接口自动化测试
- store picture
- 六、Android RILD框架
- 《Redis官方文档》Redis集群教程
- Spring Cloud构建微服务架构(三)断路器
- STC89C52RC的串口下载
- 通过优化指针实现的优先列表
- lucene3.0入门(索引的文件写入+索引关键词查找)
- JavaScript 全栈工程师培训教程(React.js + Node.js)