首先还是看前端~
Case的管理页面,由原本的Http变成支持Http、Dubbo以及Sql。
集合详情页,Case所展示的内容也做了适当调整,只会展示Case名称和Case类型。
这张表用于保存集合内的case顺序。
然后新建了一个CommonCase,caseInfo字段用于保存case的完整信息。
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
@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)》。
@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
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的执行就完成啦,有疑问的小伙伴欢迎在文章下方留言,我会根据问题不断优化文章内容!
- End -
- 软件测试君 -
我们只研究那些
你感兴趣的技术
喜欢我们就长按下方图片扫码关注吧
· 猜你喜欢的文章 ·
1、刚做测试工作一年的时候,我是怎样的?
2、请问,软件测试中,购物车的测试点有哪些?
3、四个类搞定分层自动化测试框架
4、关于接口测试看这篇文章就够了
5、python接口自动化学习笔记(封装方法用于读取excel)
▼br/>![](https://s4.51cto.com/images/blog/202011/24/ddd474e94abfabd3c5baf163e381fdb5.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
作者@简单随风
编辑@糖小幽br/>图片@来源于网络
商务合作请联系微信:sofeicoffee