从0到1,用Jmeter搭建HTTP接口自动化引擎 1.0 版本
1、已实现功能描述
1.1 框架包含:Jenkins、Ant、Jmeter
1.2 测试用例来源:.csv文件
如图:
1.3 报警邮件
1.3.1 失败用例内容:详细说明失败用例的所有信息
1.3.2 失败用例组统计及附件:
附件内容包括
1.4 自动化测试执行需要修改内容
1.4.1 编写测试用例
1.4.2 修改用例文件地址
1.4.3 修改报告附件地址
2、接口自动化引擎逻辑详解
2.1 引擎框架
2.2 引擎逻辑描述
2.2.1 全局变量设计
2.2.2 测试用例文件导入
2.2.3 http头信息管理
2.2.4 http cookie信息管理
2.2.5 判断用例文件中用例是否需要测试
2.2.6 判断http请求是post还是get
2.2.7 post请求配置逻辑
2.2.8 BeanShell断言内容
断言内容解析:
int errcount = Integer.parseInt(vars.get("errcount").trim());
int allcount = Integer.parseInt(vars.get("respcount").trim());
int equalscount=0;
int equalsokcount=0;
if("${HttpStatus}"!=""){
// log.info("==========================${HttpStatus}");
if(SampleResult.getResponseCode().equals("${HttpStatus}".trim())) {
equalscount++;
equalsokcount++;
// log.info("================equalscount: " + equalscount);
// log.info("================equalsokcount: " + equalsokcount);
}else {
String code = SampleResult.getResponseCode();
vars.put("respcode",code);
vars.put("AssertionNoOk","断言失败内容为:HttpStatus,实际请求返回的HttpStatus为: "+code+",断言内容的HttpStatus为: "+"${HttpStatus}");
equalscount++;
// log.info("================ERRORequalscount: " + equalscount);
// log.info("================ERRORequalsokcount: " + equalsokcount);
// log.info("====================ErrorHttpStatus is: " + code);
}
}
if("${ResponseMessage}"!=""){
if(SampleResult.getResponseMessage().equalsIgnoreCase("${ResponseMessage}".trim())) {
equalscount++;
equalsokcount++;
}else {
equalscount++;
String msg = SampleResult.getResponseMessage();
vars.put("respmsg",msg);
vars.put("AssertionNoOk","断言失败内容为:ResponseMessage,实际请求返回的ResponseMessage为: "+msg+",断言内容的ResponseMessage为: "+"${ResponseMessage}");
}
}
if("${ResponseData1}"!=""){
if(SampleResult.getResponseDataAsString().contains("${ResponseData1}".trim())) {
equalscount++;
equalsokcount++;
}else {
equalscount++;
String data = SampleResult.getResponseDataAsString();
vars.put("respdata",data);
vars.put("AssertionNoOk","断言失败内容为:ResponseData1,实际请求返回的ResponseData1为: "+data+",断言内容的ResponseData1为: "+"${ResponseData1}");
}
}
//log.info("===================================ResponseData2=" + "${ResponseData2}".trim());
if("${ResponseData2}"!=""){
if(SampleResult.getResponseDataAsString().contains("${ResponseData2}".trim())) {
equalscount++;
equalsokcount++;
}else {
equalscount++;
String data = SampleResult.getResponseDataAsString();
vars.put("respdata",data);
vars.put("AssertionNoOk","断言失败内容为:ResponseData2,实际请求返回的ResponseData2为: "+data+",断言内容的ResponseData2为: "+"${ResponseData2}");
}
}
if("${ResponseData3}"!=""){
if(SampleResult.getResponseDataAsString().contains("${ResponseData3}".trim())) {
equalscount++;
equalsokcount++;
}else {
equalscount++;
String data = SampleResult.getResponseDataAsString();
vars.put("respdata",data);
vars.put("AssertionNoOk","断言失败内容为:ResponseData3,实际请求返回的ResponseData3为: "+data+",\n断言内容的ResponseData3为: "+"${ResponseData3}");
}
}
if(equalscount==equalsokcount) {
SampleResult.setSuccessful(true);
vars.put("sendemail","no");
allcount++;
vars.put("respcount",allcount.toString());
}else {
errcount++;
allcount++;
SampleResult.setSuccessful(false);
vars.put("errcount",errcount.toString());
vars.put("respcount",allcount.toString());
vars.put("sendemail","yes");
}
2.2.9 判断是否发送邮件
利用SMTP Sampler发送邮件
2.2.10 get请求配置逻辑
2.2.11 所有用例执行完毕,发送报警邮件
邮件内容如下:
至此,Jmeter HTTP接口自动化引擎搭建完毕
还可以利用jenkins定时执行Jmeter引擎,从而实现对线上接口的监控
资料参考:
(1)Jmeter build.xml文件:http://pan.baidu.com/s/1i46tMlR
(2)Jmeter生成报告格式(参考heman贺满博客http://www.cnblogs.com/puresoul/http://pan.baidu.com/s/1kVssMzl):
(3)jmeter.properties(修改jmeter生成.jtl文件内容配置):http://pan.baidu.com/s/1cyaNlc
(4)测试用例模版:http://pan.baidu.com/s/1kVegq6j
(5)Jmeter接口自动化引擎内容:http://pan.baidu.com/s/1kVop9n5