接口测试平台:支持混合Case的执行(HttpDubboSql)

2020-02-12 16:48:40 浏览数 (1)

首先还是看前端~

Case的管理页面,由原本的Http变成支持Http、Dubbo以及Sql。

集合详情页,Case所展示的内容也做了适当调整,只会展示Case名称和Case类型。

这张表用于保存集合内的case顺序。

然后新建了一个CommonCase,caseInfo字段用于保存case的完整信息。

代码语言:javascript复制
public class CommonCase {

    /**
     * 前端传入的id
     * 不入库,在函数里转换为caseId
     */
    private Integer id;

    /** 集合id */
    private Integer collectionId;

    /** caseId */
    private Integer caseId;

    /** Case名称 */
    private String caseName;

    /**
     * 集合类型
     * 1:http 2:dubbo  3:mysql
     */
    private Integer caseType;

    private Object caseInfo;

}

Service层

之前传入testng的是一个httpCase,现在改成了一个commonCase

代码语言:javascript复制
@Override
public ResponseVo collectionExcute(Integer collectionId) {

   ResponseVo responseVo = new ResponseVo();

   // 获取完整的case列表
   ApiTestConfig.caseList = collectionCaseManageMapper.selectCommonCase(collectionId);

   // 遍历caseList,给caseInfo进行赋值,获取完整的case信息
   for (CommonCase commonCase:ApiTestConfig.caseList){
       Integer caseType = commonCase.getCaseType();
       switch (caseType){
           case ApiTestConfig.HTTP_CASE_TYPE:
               commonCase.setCaseInfo(httpCaseMapper.selectHttpCase(commonCase.getCaseId()));
               break;
           case ApiTestConfig.DUBBO_CASE_TYPE:
               commonCase.setCaseInfo(dubboCaseMapper.selectDubboCase(commonCase.getCaseId()));
               break;
           case ApiTestConfig.SQL_CASE_TYPE:
               SqlCase sqlCase = sqlCaseMapper.selectSqlCase(commonCase.getCaseId());
               DataBase dataBase = apiTestConfigMapper.selectDataBaseById(sqlCase.getDatabaseId());
               dataBase.setSqlCase(sqlCase);
               commonCase.setCaseInfo(dataBase);
               break;
           default:
               break;
       }
   }

   ApiTestConfig.collectionId = collectionId;

   // 全局变量赋值
   List<Variable> globalVariableList = apiTestConfigMapper.selectGlobalVariable();
   for (Variable variable:globalVariableList){
       ApiTestConfig.globalVariableMap.put(variable.getVariableName(),variable.getVariableValue());
   }

   // 集合变量赋值
   List<Variable> collectionVariableList = apiTestCollectionMapper.selectCollectionVariable(collectionId);
   for (Variable variable:collectionVariableList){
       ApiTestConfig.collectionVariableMap.put(variable.getVariableName(),variable.getVariableValue());
   }

   // 测试报告保存路径
   ApiTestConfig.reportName = String.valueOf(System.currentTimeMillis())  ".html";
   CollectionDetail collectionDetail =  apiTestCollectionMapper.selectCollectionDetail(collectionId);
   collectionDetail.setId(collectionId);
   collectionDetail.setReportPath("/report/"   ApiTestConfig.reportName);

   // 更新最后执行人和最后执行时间
   User u = HttpBasicAuthorizeAttribute.getTokenUserInfo().get();
   collectionDetail.setExcuterCode(u.getCode());
   collectionDetail.setExcuterName(u.getName());
   collectionDetail.setExcuteDatetime(new Date());
   apiTestCollectionMapper.updateCollection(collectionDetail);

   // 新增执行记录
   apiTestCollectionMapper.insertExcuteRecords(collectionDetail);

   // 测试报告推送至企业微信
   if (collectionDetail.getWxPush()){
       String content = "接口测试平台报告 n";
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       content  = "【测试时间】"   sdf.format(new Date())   "n";
       content  = "【测试报告】http://10.230.27.158:8080/report/"   ApiTestConfig.reportName;
       WxMsgPush.wxMsgPush(u.getCode(), content);
   }

   // 测试执行
   TestNG testNg = new TestNG();
   Class[] listenerClass = {ExtentTestNGIReporterListener.class};
   testNg.setListenerClasses(Arrays.asList(listenerClass));

   testNg.setTestClasses(new Class[]{CollectionDetailExcute.class});
   testNg.run();

   responseVo.setIsSuccess(Boolean.TRUE);
   responseVo.setResult("执行完毕");

   return responseVo;
}

CollectionDetailExcute

原本的http请求拓展为三种Case类型。

具体的执行代码可以参考之前的文章《接口测试平台-Http请求的简单执行》、《接口测试平台-Dubbo接口支持》、《接口测试平台-支持SQL语句执行(Mysql、Oracle)》。

代码语言:javascript复制
@Override
public ResponseVo collectionExcute(Integer collectionId) {

   ResponseVo responseVo = new ResponseVo();

   // 获取完整的case列表
   ApiTestConfig.caseList = collectionCaseManageMapper.selectCommonCase(collectionId);

   // 遍历caseList,给caseInfo进行赋值,获取完整的case信息
   for (CommonCase commonCase:ApiTestConfig.caseList){
       Integer caseType = commonCase.getCaseType();
       switch (caseType){
           case ApiTestConfig.HTTP_CASE_TYPE:
               commonCase.setCaseInfo(httpCaseMapper.selectHttpCase(commonCase.getCaseId()));
               break;
           case ApiTestConfig.DUBBO_CASE_TYPE:
               commonCase.setCaseInfo(dubboCaseMapper.selectDubboCase(commonCase.getCaseId()));
               break;
           case ApiTestConfig.SQL_CASE_TYPE:
               SqlCase sqlCase = sqlCaseMapper.selectSqlCase(commonCase.getCaseId());
               DataBase dataBase = apiTestConfigMapper.selectDataBaseById(sqlCase.getDatabaseId());
               dataBase.setSqlCase(sqlCase);
               commonCase.setCaseInfo(dataBase);
               break;
           default:
               break;
       }
   }

   ApiTestConfig.collectionId = collectionId;

   // 全局变量赋值
   List<Variable> globalVariableList = apiTestConfigMapper.selectGlobalVariable();
   for (Variable variable:globalVariableList){
       ApiTestConfig.globalVariableMap.put(variable.getVariableName(),variable.getVariableValue());
   }

   // 集合变量赋值
   List<Variable> collectionVariableList = apiTestCollectionMapper.selectCollectionVariable(collectionId);
   for (Variable variable:collectionVariableList){
       ApiTestConfig.collectionVariableMap.put(variable.getVariableName(),variable.getVariableValue());
   }

   // 测试报告保存路径
   ApiTestConfig.reportName = String.valueOf(System.currentTimeMillis())  ".html";
   CollectionDetail collectionDetail =  apiTestCollectionMapper.selectCollectionDetail(collectionId);
   collectionDetail.setId(collectionId);
   collectionDetail.setReportPath("/report/"   ApiTestConfig.reportName);

   // 更新最后执行人和最后执行时间
   User u = HttpBasicAuthorizeAttribute.getTokenUserInfo().get();
   collectionDetail.setExcuterCode(u.getCode());
   collectionDetail.setExcuterName(u.getName());
   collectionDetail.setExcuteDatetime(new Date());
   apiTestCollectionMapper.updateCollection(collectionDetail);

   // 新增执行记录
   apiTestCollectionMapper.insertExcuteRecords(collectionDetail);

   // 测试报告推送至企业微信
   if (collectionDetail.getWxPush()){
       String content = "接口测试平台报告 n";
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       content  = "【测试时间】"   sdf.format(new Date())   "n";
       content  = "【测试报告】http://10.230.27.158:8080/report/"   ApiTestConfig.reportName;
       WxMsgPush.wxMsgPush(u.getCode(), content);
   }

   // 测试执行
   TestNG testNg = new TestNG();
   Class[] listenerClass = {ExtentTestNGIReporterListener.class};
   testNg.setListenerClasses(Arrays.asList(listenerClass));

   testNg.setTestClasses(new Class[]{CollectionDetailExcute.class});
   testNg.run();

   responseVo.setIsSuccess(Boolean.TRUE);
   responseVo.setResult("执行完毕");

   return responseVo;
}

DataProvider

代码语言:javascript复制
public class DataProvider implements Iterator<Object[]> {

    /** 查询结果集 */
    List<CommonCase> caseList;

    /** 总行数 */
    private int rowNum=0;
    /** 当前行数 */
    private int curRowNo=0;

    public DataProvider(List<CommonCase> cases){

        this.caseList = cases;
        this.rowNum = caseList.size();

    }

    @Override
    public boolean hasNext() {
        if(rowNum==0||curRowNo>=rowNum){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public Object[] next() {
        CommonCase commonCase = caseList.get(curRowNo);
        Object[] o=new Object[1];
        o[0]= commonCase;
        this.curRowNo  ;
        return o;
    }
}

extentreport的配置文件部分做了点修改,修改case的name。

到此混合Case的执行就完成啦,有疑问的小伙伴欢迎在文章下方留言,我会根据问题不断优化文章内容!

0 人点赞