前言:之前扩展的ant—jmeter支持邮件附件形式上传以及邮件内容的html文件格式。
如图:
由于邮件的内容格式是详情信息,也就是说直观的显示的是case,但由于case的增加,邮件内容越来越大!
最主要的是领导们看的不是每条case的执行是否通过,关注度而是每个模块也就是每个module(涉及的所有interface的场景)整体通过率。
开发和测试同学们可以通过附件看出具体失败的case以及原因~
因此需要修改MailTask类和JavaMail类!!!
MailTask类主要做的是对于满足一定条件的(也就是case)进行归类和划分
MailTask类相对之前动核心代码如下:
case的命名规范目前我要求有2条:
也就是含有module关键字即可——>这样才会接受该case并进行统计!!!
module的名称不能为空且module名称在前module关键字前面!!!如 Login Module
具体是否符合命名规范代码如下:
if(fullTitile.toLowerCase().contains("module")==true){ Module = fullTitile.substring(0,fullTitile.toLowerCase().indexOf("module")).trim();
统计规则:
根据module值分类并统计
代码如下:
if(fullTitile.toLowerCase().contains("module")==true){ Module = fullTitile.substring(0,fullTitile.toLowerCase().indexOf("module")).trim(); if(data.get(Module) == null){ count = count +1; color = (count%2<1)?color1:color2; //记录耗时 time = Integer.parseInt(line.split("\"")[1]); Entity entity = new Entity(); if (line.indexOf(" s=\"true\"") !=-1) { entity.successNum =x 1; }else{ entity.failNum = 1; } data.put(Module, entity); }else{ //记录耗时 time += Integer.parseInt(line.split("\"")[1]); Entity entity = data.get(Module); if (line.indexOf(" s=\"true\"") !=-1) { entity.successNum += 1; }else{ entity.failNum += 1; } data.put(Module, entity); } } } } br.close(); isr.close(); fis.close(); Iterator iterator = data.keySet().iterator(); while(iterator.hasNext()){ Module = (String)iterator.next(); Entity entity = data.get(Module); allSuccess+=entity.successNum; allFailure+=entity.failNum; CaseTotalNum =entity.successNum+entity.failNum;
之后重写编辑html格式:
代码如下:
String htmlString = "<tr valign=\"top\">" + "<th>Module</th>" + "<th>TotalNum</th>" + "<th>PassNum</th>" + "<th>FaliNum</th>" + "<th>PassRate</th>" + "</tr>";
if(CaseTotalNum == 0){ rate = "0"; htmlString = htmlString +"<tr valign=\"middle\" style=\"background:"+color+";line-height:2em;\">" + "<td align=\"center\">"+Module+"</td>" + "<td align=\"center\">"+CaseTotalNum+"</td>" + "<td align=\"center\">"+entity.successNum+"</td>" + "<td align=\"center\">"+entity.failNum+"</td>" + "<td align=\"center\">"+rate+"</td>" + "</tr>"; } else { DecimalFormat df = new DecimalFormat("0.00"); rate = df.format((float)entity.successNum/(float)CaseTotalNum*100); htmlString = htmlString +"<tr valign=\"middle\" style=\"background:"+color+";line-height:2em;\">" + "<td align=\"center\">"+Module+"</td>" + "<td align=\"center\">"+CaseTotalNum+"</td>" + "<td align=\"center\">"+entity.successNum+"</td>" + "<td align=\"center\">"+entity.failNum+"</td>" + "<td align=\"center\">"+rate+"%</td>" + "</tr>"; }
JavaMail类代码如下:
JavaMail类代码修改较少,只是修改了邮件的html报告模板,相对增加和删除了一些关键字
html代码如下:
bp.setContent("<!DOCTYPE html>" + "<html lang=\"en\">" + "<head><META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + "<meta content=\"shanhe.me\" name=\"Author\">" + "<title>JMeter Test Results</title>" + "<style type=\"text/css\">" + "* { margin: 0; padding: 0 }" + "html{font-size:14px;width:300px;height:auto;border:1px;margin: auto; }" + "body { width: 20%; margin: 0 auto; text-align:center; font-size:10%;}" + "table { font-size:14px;font-weight:normal ;border-collapse: collapse; table-layout: fixed;word-wrap:break-word;word-break:break-all;}" + "th{border:2px solid black;color: #FFFFFF;font-weight:normal;text-align:center;background:#527F76;}" + "td {border:2px solid black;font-weight:normal;}" + "</style></head></head><body>" + "<h2>Interface Test Report Summary</h2>" + "<h2 style=\"color:#5F9F9F\" >Check Attachment For Detail</h2>" + "<table width=\"85%\"class=\"details\" align=\"center\">" + "<tr style=\"line-height:2em;\" valign=\"middle\">" + "<th>Build Version</th>" + "<th>Total Statistics</th>" + "<th>Fail Statistics</th>" + "<th>Run Pass Rate</th>" + "<th>Elapsed Time</th>" + "</tr>" + "<tr style=\"line-height:2em;\" valign=\"middle\">" + "<td align=\"center\">"+bulidnum+"</td>" + "<td align=\"center\">"+all+"</td>" + "<td align=\"center\">"+failnum+"</td>" + "<td align=\"center\">"+s+"%</td>" + "<td align=\"center\">"+time+"</td>" + "</tr>" +htmlstring + "</table></body></html>", "text/html;charset=utf-8"); mp.addBodyPart(bp);
sendMail 增加了总耗时参数属性
打包完成,发邮件报告如下:
另说明:
由于之前只对http请求进行统计,现增加了jdbc请求,所以也要想把http请求统计进去的话,
由于jdbc请求在jtl文件生成的格式如下
<sample t="646" it="0" lt="633" ct="0" ts="1497606686753" s="true" lb="Jdbc Module Request table configuration precondition ContentType=19" rc="200" rm="OK" dt="text" de="UTF-8" by="404" sc="1" ec="0" ng="1" na="1" hn="nj-zoe-yang4">
http请求在jtl文件生成的格式如下:
<httpSample t="614" it="0" lt="614" ct="180" ts="1497606687694" s="true" lb="Login Module login Interface get cookie correct parameter" rc="200" rm="OK" dt="text" de="utf-8" by="509" sc="1" ec="0" ng="1" na="1" hn="nj-zoe-yang4">
代码修改如下
while ((line = br.readLine()) != null) {
if (line.indexOf("<httpSample") !=-1||line.indexOf("<sample t=") !=-1) {
fullTitile = line.split("\"")[13];
有更好的html模板大家可以跟我沟通下,本人html很菜~也不愿意去接触~
修改错误:
count = count +1;
color = (count%2<1)?color1:color2;
由于这个判断是无法实现奇数色值为#FFFFFF,偶数色值为#E1F3FE
因此,提取到外面计算如下:
while(iterator.hasNext()){ Module = iterator.next(); Entity entity = data.get(Module); allSuccess+=entity.successNum; allFailure+=entity.failNum; CaseTotalNum =entity.successNum+entity.failNum; lineNum += 1; // System.out.println("行数:"+lineNum); if(CaseTotalNum == 0){ rate = "0"; }else { rate = df.format((float)entity.successNum/(float)CaseTotalNum*100); } //区分每隔颜色 color = (lineNum%2!=0)?color1:color2;
后续又把总耗时单位ms改为s,如下:
alltime = df.format((float)time/1000)+"s";
报告样式为: