Jenkins Jmeter Ant Email Html实施方案

项目做接口自动化,选用的方案为Jenkins + Jmeter + Ant + Email;本篇文档是对实施的总结

优化点是:默认生成两份报告

邮件效果图:

    

第一种报告:

    

第二种报告:

    

环境准备:

  CentOS7

  JDK1.8

  ant:下载到服务器上,配置好环境变量

    vim /etc/profile  

export ANT_HOME=/data/apache-ant-1.10.5
export PATH=$ANT_HOME/bin:$PATH

Jmeter配置

   参考:  

     Jmeter教程索引贴      

       Jmeter默认报告优化

       Jmeter报告优化之New XSL stylesheet

     接口自动化Jmeter

       JMeter(6) jenkins测试报告及邮件优化

   1.下载Jmeter放在Jenkins服务器对应目录,注意修改项目build.xml

      

   2.下载jmeter.results.shanhe.me.xsl到Jmeter/extras/

      

   3.修改jmeter.properties

 1 jmeter.save.saveservice.data_type=true
 2 jmeter.save.saveservice.label=true
 3 jmeter.save.saveservice.response_code=true
 4 # response_data is not currently supported for CSV output
 5 jmeter.save.saveservice.response_data=true
 6 # Save ResponseData for failed samples
 7 jmeter.save.saveservice.response_data.on_error=false
 8 jmeter.save.saveservice.response_message=true
 9 jmeter.save.saveservice.successful=true
10 jmeter.save.saveservice.thread_name=true
11 jmeter.save.saveservice.time=true
12 jmeter.save.saveservice.subresults=true
13 jmeter.save.saveservice.assertions=true
14 jmeter.save.saveservice.latency=true
15 jmeter.save.saveservice.connect_time=true
16 jmeter.save.saveservice.samplerData=true
17 jmeter.save.saveservice.responseHeaders=true
18 jmeter.save.saveservice.requestHeaders=true
19 jmeter.save.saveservice.encoding=false
20 jmeter.save.saveservice.bytes=true
21 jmeter.save.saveservice.url=true
22 jmeter.save.saveservice.filename=true
23 jmeter.save.saveservice.hostname=true
24 jmeter.save.saveservice.thread_counts=true
25 jmeter.save.saveservice.sample_count=true
26 jmeter.save.saveservice.idle_time=true
View Code

Jenkins的安装配置

1 wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
2 rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
3 yum install jenkins

  官方地址:https://pkg.jenkins.io/redhat-stable/

  参考:

    CentOS 安装 Jenkins

    jenkins邮件发送jmeter接口测试报告

    Jenkins发送测试报告邮件

    Jenkins+Jmeter+Ant自动化集成及邮件正文以html输出

    Jenkins 配置邮件通知

  因权限问题,我将Jenkins用户更改为root,随便端口更改为8081,Jenkins的主目录则未做改变。

JENKINS_HOME="/var/lib/jenkins"
JENKINS_USER="root" JENKINS_PORT="8081"

  需要的Jenkins插件:

    Email Extension Template Plugin

  Jenkins系统设置设置Email信息

    系统管理员邮件地址一定要设置,否则会导致邮件发不出去。

      

   配置Extended E-mail Notification

      注意: 密码一般应为对应邮箱服务器的授权密码

          Jenkins自带的邮件通知也挺好用的!

     

  Jenkins中配置Jmeter工程

    构建一个自由风格的软件项目

    项目配置如下:

      Jmeter脚本直接丢在Jenkins服务器上,此处保持路径一致即可,如使用SVN/GIT请另行配置

      Jmeter脚本与build.xml文件放一个目录

      我的build.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project name="ant-jmeter-test" default="run" basedir=".">
 3     <!-- 需要改成自己本地的 Jmeter 目录-->  
 4     <property name="jmeter.home" value="/data/apache-jmeter-4.0" />
 5     <!-- jmeter生成jtl格式的结果报告的路径--> 
 6     <property name="jmeter.result.jtl.dir" value="/data/apiTest" />
 7     <!-- jmeter生成html格式的结果报告的路径-->
 8     <property name="jmeter.result.html.dir" value="/data/apiTest" />
 9     <!-- 生成的报告的前缀-->  
10     <property name="ReportName" value="passengerApiReport" />
11     <property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${ReportName}.jtl" />
12     <property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${ReportName}.html" />
13     <property name="jmeter.result.htmlInfo" value="${jmeter.result.html.dir}/${ReportName}Info.html" />
14     
15     <target name="run">
16         <antcall target="test" />
17         <antcall target="report" />
18     </target>
19     
20     <target name="test">
21         <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
22         <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
23              <!-- 声明要运行的脚本"*.jmx"指包含此目录下的所有jmeter脚本-->
24             <testplans dir="/data/apiTest" includes="*.jmx" />
25         <property name="jmeter.save.saveservice.output_format" value="xml"/>
26         </jmeter>
27     </target>
28 
29     <path id="xslt.classpath">
30         <fileset dir="${jmeter.home}/lib" includes="xalan*.jar"/>
31         <fileset dir="${jmeter.home}/lib" includes="serializer*.jar"/>
32     </path>
33 
34         
35     <target name="report">
36     <tstamp> <format property="report.datestamp" pattern="yyyy/MM/dd HH:mm" /></tstamp>
37         <xslt 
38               classpathref="xslt.classpath"
39               force="true"
40               in="${jmeter.result.jtlName}"
41               out="${jmeter.result.htmlName}"
42               style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl">
43               <param name="dateReport" expression="${report.datestamp}" />
44         </xslt>
45     <xslt
46               classpathref="xslt.classpath"
47               force="true"
48               in="${jmeter.result.jtlName}"
49               out="${jmeter.result.htmlInfo}"
50               style="${jmeter.home}/extras/jmeter.results.shanhe.me.xsl">
51               <param name="dateReport" expression="${report.datestamp}" />
52         </xslt>
53         <!-- 因为上面生成报告的时候,不会将相关的图片也一起拷贝至目标目录,所以,需要手动拷贝 --> 
54         <copy todir="${jmeter.result.html.dir}">
55             <fileset dir="${jmeter.home}/extras">
56                 <include name="collapse.png" />
57                 <include name="expand.png" />
58             </fileset>
59         </copy>
60     </target>
61 </project>
View Code

 

        

    每天08:50进行接口测试

      

    构建方式为进入对应路径,执行ant

cd /data/apiTest
/data/apache-ant-1.10.5/bin/ant

      

  邮件模板

    我的邮件模板:

 1 <!DOCTYPE html>
 2 <html>
 3 
 4 <head>
 5     <meta charset="UTF-8">
 6     <title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>
 7 </head>
 8 
 9 <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
10     <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
11         <tr>
12             <td>(本邮件是程序自动下发的,请勿回复!)</td>
13         </tr>
14         <tr>
15             <td>
16                 <h2> <font color="#0000FF">XXXXX接口测试结果 - 以附件报告为准(请使用浏览器打开)</font> </h2></td>
17         </tr>
18         <tr>
19             <td>
20                 <br /> <b><font color="#0B610B">构建信息</font></b>
21                 <hr size="2" width="100%" align="center" />
22             </td>
23         </tr>
24         <tr>
25             <td>
26                 <ul>
27                     <li>项目名称 : ${PROJECT_NAME}</li>
28                     <li>构建编号 : 第${BUILD_NUMBER}次构建</li>
29                     <li>触发原因: ${CAUSE}</li>
30                     <li>构建日志: <a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
31                     <li>构建 Url : <a href="${BUILD_URL}">${BUILD_URL}</a></li>
32                     <li>工作目录 : <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
33                     <li>项目 Url : <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
34                 </ul>
35             </td>
36         </tr>
37         <tr>
38             <td><b><font color="#0B610B">Changes Since Last Successful Build:</font></b>
39                 <hr size="2" width="100%" align="center" />
40             </td>
41         </tr>
42         <tr>
43             <td>
44                 <ul>
45                     <li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
46         </tr>
47         <tr>
48             <td><b>Failed Test Results</b>
49                 <hr size="2" width="100%" align="center" />
50             </td>
51         </tr>
52         <tr>
53             <td><pre style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre>
54                 <br />
55             </td>
56         </tr>
57         <tr>
58             <td><b><font color="#0B610B">构建日志 (最后 100行):</font></b>
59                 <hr size="2" width="100%" align="center" />
60             </td>
61         </tr>
62         <!-- <tr> <td>Test Logs (if test has ran): <a href="${PROJECT_URL}ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip">${PROJECT_URL}/ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip</a> <br /> <br /> </td> </tr> -->
63         <tr>
64             <td>
65                 <textarea cols="80" rows="30" readonly="readonly" style="font-family: Courier New">${BUILD_LOG, maxLines=100}</textarea>
66             </td>
67         </tr>
68     </table>
69 </body>
70 
71 </html>
View Code

    

    

 

    

 

posted @ 2018-08-10 09:37  Acumen  阅读(203)  评论(0编辑  收藏  举报