【Jmeter篇】jmeter+Ant+Jenkins实现自动化测试集成(一)
一.簡介
1、什么是ant????
ant是構建工具,把代碼從某個地方拿來,編譯,再拷貝到某個地方去等等操作
JMeter+Ant是比較常見的自動化測試框架,因為JMeter、Ant都是由java開發的,所以此性能測試框架具有良好的跨平臺性;下圖是按自己的理解繪制的自動化框架圖:
2、ant的好處
???跨平臺???--因為ant是使用java實現的,所以它跨平臺
???使用簡單--與ant的兄弟make比起來
???語法清晰--同樣是和make相比
???功能強大--ant能做的事情很多,可能你用了很久,你仍然不知道它能有多少功能。當你自己開發一些ant插件的時候,你會發現它更多的功能。
3、實現過程
(1)Ant通過XML文件進行構建,所有的構建信息配置在build.xml文件中,通過調用target樹來執行各種任務;如執行測試腳本(jmx文件),輸出測試結果(jtl文件);
(2)Ant通過測試結果(jtl文件)構建生成html測試報告,輸出在制定路徑下;
(3)根據build.xml文件中配置的郵件信息,將html測試報告自動發送到制定的郵箱;
4、原理
jenkins驅動ant執行,ant驅動jmeter執行
二.配置ant環境
1、去Apache官網上http://ant.apache.org/bindownload.cgi下載ant,我下載的是apach-ant-1.10.7?
2、下完之后解壓,放到一個目錄下面。我把它和jmeter都放在了一個目錄下C:\jmeter\apache-ant-1.10.7
3、配置環境變量,與jmeter類似。?
? ?新建系統變量ANT_HOME,值C:\jmeter\apache-ant-1.10.7
? ?系統變量CLASSPATH,值后面添加C:\jmeter\apache-ant-1.10.7\lib
? ?系統變量Path,值后面添加C:\jmeter\apache-ant-1.10.7\bin
cmd下,輸入ant,結果如下圖所示,"build.xml does not exist "說明ant配置成功
三.Jmeter與Ant集成使用
準備環境:jdk1.8.0_60,環境變量的配置;jmeter5.0安裝,環境變量的配置;ant1.10.7的安裝,環境變量的配置
1、將C:\jmeter\apache-jmeter-5.0\extras文件夾下的ant-jmeter-1.1.1.jar這個jar包復制到C:\jmeter\apache-ant-1.10.7\lib文件夾下
2、打開C:\jmeter\apache-jmeter-5.0\bin\jmeter.properties?將jmeter.save.saveservice.output_format=csv? 改成? ?jmeter.save.saveservice.output_format=xml,記得去掉前面的“#”
3、.jmx腳本放置C:\jmeter\apache-jmeter-5.0\extras下
4、進入C:\jmeter\apache-jmeter-5.0\extras,打開build.xml文件,進行配置
第一處要修改的地方
<!-- Name of test (without .jmx) -->
<property name="test1" value="insight2.0-180720"/>
此處填寫你要測試的腳本,后面不用寫.jmx,只用寫文件名。這里的意思為test1的值為insight2.0-180720,你將要測試的腳本就是insight2.0-180720.jmx
當然property name的也可以取其他的,test、a、b.....等等都可以,簡單理解為一個變量名,value就是變量值。
第二處要修改的地方
在<target name = "run"> 下面修改子標簽jmeter
<jmeter
jmeterhome="${jmeter.home}"
testplan ="${testpath}/${test1}.jmx"
resultlog="${testpath}/${test1}.jtl">
........
</jmeter>
這里就開始運行jmx腳本了,測試計劃testplan的值就是上一處你定義的test1;
resultlog就是執行jmx之后生成的日志文件,它會默認保存在extras文件夾下。
第三處要修改的地方
在<target name="xslt-report" depends="_message_xalan">下面修改子標簽xslt
<xslt
classpathref="xslt.classpath"
force="true"
in="${testpath}/${test1}.jtl"
out="${testpath}/${test1}.html"
........
</xslt>
這里做的工作是,將jmeter執行之后生成的jtl文件,編譯構建成HTML文件,默認保存在extras文件夾下。
仔細看,in等于的就是上一處的rasultlog,out的后綴為html
第四處要修改的地方
我想每執行一次腳本,就生成一個HTML、一個jtl文件,并且文件名能顯示每次執行的時間。這里可以聯想到時間戳,每次運行結果都生成不一樣的文件。
1、定義指定格式的時間變量。可以直接在target name =run下定義
<tstamp>
<format property="time" pattern="yyyy-MM-dd-HH-mm-ss"/>
</tstamp>
2、在jmeter標簽下,修改jtl文件的存儲路徑
<jmeter
jmeterhome="${jmeter.home}"
testplan ="${testpath}/${test1}.jmx"
resultlog="${testpath}/${test1}/${time}.jtl">
...</jmeter>
3、在xslt標簽下,修改in、out的路徑
<xslt
classpathref="xslt.classpath"
force="true"
in="${testpath}/${test1}/${time}.jtl"
out="${testpath}/${test1}/${time}.html"
.....</xslt>
?
第五處要修改的地方
build.xml文件無法使用*.jmx執行extras目錄下所有的jmx文件,那么如果想每一次想執行多個腳本怎么辦呢??目前想到了一個解決辦法。大概思路就是,定義多個變量,分別執行。
<!-- Name of test (without .jmx) -->
<property name="test1" value="要執行的腳本1"/>
<property name="test2" value="要執行的腳本2"/>
<property name="test3" value="要執行的腳本3"/>
.........
5、修改完build.xml文件之后,就可以試一下jmeter+ant的集成有沒有成功,打開命令提示符,進入到C:\jmeter\apache-jmeter-5.0\extras,在這里輸入ant。
最后面顯示build successful說明執行成功了!可以進入到extras文件夾下面查看結果,生成了HTML和jtl文件
6、打開html測試報告如下
7、聚合報告打開jtl文件如下
8、build.xml內容如下
<?xml version="1.0" encoding="utf-8"?> <!--Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements. See the NOTICE file distributed withthis work for additional information regarding copyright ownership.The ASF licenses this file to You under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance withthe License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License. --> <project name="ant-jmeter" default="all"><description>Sample build file for use with ant-jmeter.jarSee http://www.programmerplanet.org/pages/projects/jmeter-ant-task.phpTo run a test and create the output report:ant -Dtest=scriptTo run a test only:ant -Dtest=script runTo run report on existing test outputant -Dtest=script reportThe "script" parameter is the name of the script without the .jmx suffix.Additional options:-Dshow-data=y - include response data in Failure Details-Dtestpath=xyz - path to test file(s) (default user.dir).N.B. Ant interprets relative paths against the build file-Djmeter.home=.. - path to JMeter home directory (defaults to parent of this build file)-Dreport.title="My Report" - title for html report (default is 'Load Test Results')</description><property name="testpath" value="${user.dir}"/><property name="jmeter.home" value="${basedir}/.."/><property name="report.title" value="Load Test Results"/><!-- Name of test (without .jmx) --><property name="test1" value="insight2.0-180720"/><!-- Should report include response data for failures? --><property name="show-data" value="n"/><property name="format" value="2.1"/><condition property="style_version" value="_21"><equals arg1="${format}" arg2="2.1"/></condition><condition property="funcMode"><equals arg1="${show-data}" arg2="y"/></condition><condition property="funcMode" value="false"><not><equals arg1="${show-data}" arg2="y"/></not></condition><!-- Allow jar to be picked up locally --><path id="jmeter.classpath"><fileset dir="${basedir}"><include name="ant-jmeter*.jar"/></fileset></path><taskdefname="jmeter"classpathref="jmeter.classpath"classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/><target name="all" depends="run,report"/><target name="run"><echo>funcMode = ${funcMode}</echo><tstamp><format property="time" pattern="yyyy-MM-dd-HH-mm-ss"/></tstamp><delete file="${testpath}/${test}.html"/><jmeterjmeterhome="${jmeter.home}"testplan ="${testpath}/${test1}.jmx"resultlog="${testpath}/${test1}/${time}.jtl"><!--<jvmarg value="-Xincgc"/><jvmarg value="-Xmx128m"/><jvmarg value="-Dproperty=value"/><jmeterarg value="-qextra.properties"/>--><!-- Force suitable defaults --><property name="jmeter.save.saveservice.output_format" value="xml"/><property name="jmeter.save.saveservice.assertion_results" value="all"/><property name="jmeter.save.saveservice.bytes" value="true"/><property name="file_format.testlog" value="${format}"/><property name="jmeter.save.saveservice.response_data.on_error" value="${funcMode}"/></jmeter></target><property name="lib.dir" value="${jmeter.home}/lib"/><!-- Use xalan copy from JMeter lib directory to ensure consistent processing with Java 1.4+ --><path id="xslt.classpath"><fileset dir="${lib.dir}" includes="xalan*.jar"/><fileset dir="${lib.dir}" includes="serializer*.jar"/></path><target name="report" depends="xslt-report,copy-images"><echo>Report generated at ${report.datestamp}</echo></target><target name="xslt-report" depends="_message_xalan"><tstamp><format property="report.datestamp" pattern="yyyy/MM/dd HH:mm"/></tstamp><xsltclasspathref="xslt.classpath"force="true"in="${testpath}/${test1}/${time}.jtl"out="${testpath}/${test1}/${time}.html"style="${basedir}/jmeter-results-detail-report${style_version}.xsl"><param name="showData" expression="${show-data}"/><param name="titleReport" expression="${report.title}"/><param name="dateReport" expression="${report.datestamp}"/></xslt></target><!-- Copy report images if needed --><target name="copy-images" depends="verify-images" unless="samepath"><copy file="${basedir}/expand.png" tofile="${testpath}/expand.png"/><copy file="${basedir}/collapse.png" tofile="${testpath}/collapse.png"/></target><target name="verify-images"><condition property="samepath"><equals arg1="${testpath}" arg2="${basedir}" /></condition></target><!-- Check that the xalan libraries are present --><condition property="xalan.present"><and><!-- No need to check all jars; just check a few --><available classpathref="xslt.classpath" classname="org.apache.xalan.processor.TransformerFactoryImpl"/><available classpathref="xslt.classpath" classname="org.apache.xml.serializer.ExtendedContentHandler"/></and></condition><target name="_message_xalan" unless="xalan.present"><echo>Cannot find all xalan and/or serialiser jars</echo><echo>The XSLT formatting may not work correctly.</echo><echo>Check you have xalan and serializer jars in ${lib.dir}</echo></target></project>?
?
?
?
總結
以上是生活随笔為你收集整理的【Jmeter篇】jmeter+Ant+Jenkins实现自动化测试集成(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【功能业务篇】APP获取用户定位、传参经
- 下一篇: 构建测试的体系化思维(进阶篇)