搭建持續整合介面測試平臺(Jenkins+Ant+Jmeter)

五柳-先生發表於2016-02-22

一、環境準備:

1、JDK:http://www.oracle.com/technetwork/java/javase/downloads/index.html

2、Jmeter:http://jmeter.apache.org/download_jmeter.cgi

3、Ant:http://ant.apache.org/bindownload.cgi

4、Jenkins:http://jenkins-ci.org/

具體可參見我另一篇部落格:http://www.cnblogs.com/puresoul/p/4808416.html

  

二、Jemter指令碼準備:

1、指令碼目錄:D:\B_TOOLS\apache-jmeter-2.13\demo

2、指令碼內容:都是簡單的訪問百度或google首頁

baidu.jmx                      google.jmx

 

 

三、ANT的build.xml程式碼準備:

build3.xml 

複製程式碼
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <project name="ant-jmeter-test" default="run" basedir=".">
 4     <tstamp>
 5         <format property="time" pattern="yyyyMMddhhmm" />
 6     </tstamp>
 7 
 8     <property environment="env"/>
 9     <property name="ReportName" value="TestReport" />
10     <!-- 需要改成自己本地的 Jmeter 目錄--> 
11     <property name="jmeter.home" value="D:\B_TOOLS\apache-jmeter-2.13" />
12     <!-- jmeter生成jtl、html格式的結果報告的路徑--> 
13     <property name="jmeter.result.dir" value="${env.WORKSPACE}/results/${env.BUILD_ID}" />
14     <!-- 生成的報告的字首-->  
15     <property name="jmeter.result.jtlName" value="${jmeter.result.dir}/${ReportName}.jtl" />
16     <property name="jmeter.result.htmlName" value="${jmeter.result.dir}/${ReportName}.html" />
17     
18     <target name="run">
19         <echo message="start..."/>
20         <antcall target="clean" />
21         <antcall target="test" />
22         <antcall target="report" />
23     </target>
24     
25     <target name="clean">
26         <mkdir dir="${env.WORKSPACE}/results/${env.BUILD_ID}" />
27     </target>
28         
29     <target name="test">
30         <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
31         <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
32             <!-- 宣告要執行的指令碼"*.jmx"指包含此目錄下的所有jmeter指令碼-->
33             <testplans dir="D:\B_TOOLS\apache-jmeter-2.13\demo" includes="*.jmx" />
34             
35             <property name="jmeter.save.saveservice.output_format" value="xml"/>
36         </jmeter>
37     </target>
38         
39     <target name="report">
40         <xslt in="${jmeter.result.jtlName}"
41               out="${jmeter.result.htmlName}"
42               style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl" />
43         <!-- 因為上面生成報告的時候,不會將相關的圖片也一起拷貝至目標目錄,所以,需要手動拷貝 --> 
44         <copy todir="${jmeter.result.dir}">
45             <fileset dir="${jmeter.home}/extras">
46                 <include name="collapse.png" />
47                 <include name="expand.png" />
48             </fileset>
49         </copy>
50     </target>
51 </project>
複製程式碼

  

四、配置Jenkins Job並執行:

1、job配置如下:

2、job執行結果: 

複製程式碼
 1 Started by user anonymous
 2 Building in workspace D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace
 3 [demo] $ cmd.exe /C '"ant.bat -file build3.xml run && exit %%ERRORLEVEL%%"'
 4 Buildfile: build3.xml
 5 
 6 run:
 7      [echo] start...
 8 
 9 clean:
10     [mkdir] Created dir: D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15
11 
12 test:
13    [jmeter] Executing test plan: D:\B_TOOLS\apache-jmeter-2.13\demo\baidu.jmx ==> D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15\TestReport.jtl
14    [jmeter] Creating summariser <summary>
15    [jmeter] Created the tree successfully using D:\B_TOOLS\apache-jmeter-2.13\demo\baidu.jmx
16    [jmeter] Starting the test @ Tue Sep 22 15:50:33 CST 2015 (1442908233010)
17    [jmeter] Waiting for possible shutdown message on port 4445
18    [jmeter] summary +      1 in     1s =    2.0/s Avg:   254 Min:   254 Max:   254 Err:     0 (0.00%) Active: 1 Started: 1 Finished: 0
19    [jmeter] summary +      5 in     1s =    7.3/s Avg:   131 Min:    96 Max:   196 Err:     0 (0.00%) Active: 0 Started: 1 Finished: 1
20    [jmeter] summary =      6 in   1.2s =    5.0/s Avg:   151 Min:    96 Max:   254 Err:     0 (0.00%)
21    [jmeter] Tidying up ...    @ Tue Sep 22 15:50:34 CST 2015 (1442908234310)
22    [jmeter] ... end of run
23    [jmeter] Executing test plan: D:\B_TOOLS\apache-jmeter-2.13\demo\google.jmx ==> D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15\TestReport.jtl
24    [jmeter] Creating summariser <summary>
25    [jmeter] Created the tree successfully using D:\B_TOOLS\apache-jmeter-2.13\demo\google.jmx
26    [jmeter] Starting the test @ Tue Sep 22 15:50:35 CST 2015 (1442908235240)
27    [jmeter] Waiting for possible shutdown message on port 4445
28    [jmeter] summary +      1 in     1s =    1.9/s Avg:   253 Min:   253 Max:   253 Err:     1 (100.00%) Active: 1 Started: 1 Finished: 0
29    [jmeter] summary +      5 in   1.1s =    4.4/s Avg:   209 Min:    97 Max:   420 Err:     0 (0.00%) Active: 0 Started: 1 Finished: 1
30    [jmeter] summary =      6 in     2s =    3.6/s Avg:   216 Min:    97 Max:   420 Err:     1 (16.67%)
31    [jmeter] Tidying up ...    @ Tue Sep 22 15:50:36 CST 2015 (1442908236992)
32    [jmeter] ... end of run
33 
34 report:
35      [xslt] Processing D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15\TestReport.jtl to D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15\TestReport.html
36      [xslt] Loading stylesheet D:\B_TOOLS\apache-jmeter-2.13\extras\jmeter-results-detail-report_21.xsl
37      [copy] Copying 2 files to D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15
38 
39 BUILD SUCCESSFUL
40 Total time: 5 seconds
41 [workspace] $ cmd /c call D:\Users\heman793\AppData\Local\Temp\hudson4765606743104861298.bat
42 
43 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace>echo D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace 
44 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace
45 
46 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace>echo 15 
47 15
48 
49 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace>cd results 
50 
51 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results>cd 15 
52 
53 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15>exit 0 
54 Finished: SUCCESS
複製程式碼

3、在job的workspace目錄下會生成結果報告:

4、TestReport.html:

更新補充:

  上面生成的報告有如下兩個問題,解決辦法參見Jmeter自帶報告優化(一)

  1、Date report這裡的時間沒有正確顯示出來

  2、Summary裡的欄位Min Time和Max Time顯示的是NaN,沒有顯示正確的時間。

 

五、配置傳送郵件功能

 1、網上下載sendmail.jar包,放在job的workspace目錄中

 2、jenkins增加構建步驟

  a)進入到測試報告的目錄

  b) 呼叫sendmail.jar命令傳送郵件

 說明:

  1、由build3.xml的第12、13行可知,報告檔案生成目錄為:${env.WORKSPACE}/results/${env.BUILD_ID},所以這裡我要先cd到具體執行的那個build_id目錄下。

   2、我們也可以把上面的兩行命令寫在成一個批處理檔案,例如我第1步有個sendmail.bat檔案就是的,然後呼叫時直接寫sendmail.bat就好了。

 

另外,我本文所有指令碼都在我本地,其實更好的辦法是使用SVN統一管理,這裡就不介紹了,感覺的可以自己研究下。


作者:Glen.He 
出處:http://www.cnblogs.com/puresoul/ 
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。

相關文章