Jenkins+maven+jmeter接口可持续集成自动化测试

Posted 程序员技术前沿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jenkins+maven+jmeter接口可持续集成自动化测试相关的知识,希望对你有一定的参考价值。

说到接口我们都能想到jmeter这个工具,但是我们如何借助jenkins来做可持续集成自动化的测试呢?下面就是我对jenkinsmavenjmeter做的可持续化集成环境搭建的学习心得。

一,jmeter脚本录制

   这一步就是录制Jmeter脚本,确保脚本能正确运行。

二,建立一个maven工程


1,打开eclipse,点击File->New->Project->Maven Project

 

 

 Jenkins+maven+jmeter接口可持续集成自动化测试

2,创建maven工程后,在工程目录下新建所需的文件夹

--Src/test/resources --存放报告模板文件

--src/main/

--src/test/jmeter        --存放jmeter脚本文件跟一些接口调用的参数

--target/jmeter/bin        --target存放结果的根目录

--target/jmeter/html

--target/jmeter/html1

--target/jmeter/lib

--target/jmeter/logs

--target/jmeter/results

--target/maven-archiver

--target/surefire

 Jenkins+maven+jmeter接口可持续集成自动化测试


3,编辑pom.xml文件

[plain] view plain copy

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  

  2.     <modelversion>4.0.0</modelversion>  

  3.     <groupid>testDemo</groupid>  

  4.     <artifactid>bluemoon</artifactid>  

  5.     <version>1.11</version>  

  6.     <name>bluemoon Maven Webapp</name>  

  7.     <url>http://maven.apache.org</url>  

  8.     <dependencies>  

  9.         <dependency>  

  10.             <groupid>junit</groupid>  

  11.             <artifactid>junit</artifactid>  

  12.             <version>3.8.1</version>  

  13.             <scope>test</scope>  

  14.         </dependency>  

  15.         <dependency>  

  16.             <groupid>javax.servlet</groupid>  

  17.             <artifactid>servlet-api</artifactid>  

  18.             <version>3.0-alpha-1</version>  

  19.         </dependency>  

  20.         <dependency>  

  21.             <groupid>mysql</groupid>  

  22.             <artifactid>mysql-connector-java</artifactid>  

  23.             <version>5.1.13</version>  

  24.         </dependency>  

  25.         <!-- <dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_jdbc</artifactId>   

  26.             <version>2.11</version> </dependency> <dependency> <groupId>com.oracle</groupId>   

  27.             <artifactId>ojdbc14</artifactId> <version>10.2.0.5</version> </dependency> -->  

  28.         <!-- MySql 5.5 Connector -->  

  29.     </dependencies>  

  30.     <properties>  

  31.         <jmeter.result.jtl.dir>${project.build.directory}jmeter esults</jmeter.result.jtl.dir>  

  32.         <jmeter.result.html.dir>${project.build.directory}jmeterhtml</jmeter.result.html.dir>  

  33.         <jmeter.result.html.dir1>${project.build.directory}jmeterhtml1</jmeter.result.html.dir1>  

  34.         <reportname>TestReport</reportname>  

  35.     </properties>  

  36.     <pluginrepositories>  

  37.         <pluginrepository>  

  38.             <id>Codehaus repository</id>  

  39.             <url>http://repository.codehaus.org/</url>  

  40.             <releases>  

  41.                 <enabled>true</enabled>  

  42.                 <updatepolicy>always</updatepolicy>  

  43.             </releases>  

  44.             <snapshots>  

  45.                 <enabled>true</enabled>  

  46.                 <updatepolicy>always</updatepolicy>  

  47.             </snapshots>  

  48.         </pluginrepository>  

  49.     </pluginrepositories>  

  50.     <build>  

  51.         <finalname>bluemoon</finalname>  

  52.         <plugins>  

  53.             <plugin>  

  54.                 <groupid>com.lazerycode.jmeter</groupid>  

  55.                 <artifactid>jmeter-maven-plugin</artifactid>  

  56.                 <version>1.10.0</version>  

  57.                 <configuration>  

  58.                     <resultsfileformat>xml</resultsfileformat>  

  59.                     <ignoreresultfailures>true</ignoreresultfailures>  

  60.                     <testresultstimestamp>flase</testresultstimestamp>  

  61.                 </configuration>  

  62.                 <executions>  

  63.                     <execution>  

  64.                         <id>test</id>  

  65.                         <phase>verify</phase>  

  66.                         <goals>  

  67.                             <goal>jmeter</goal>  

  68.                         </goals>  

  69.                         <!-- <configuration> <resultsFileFormat>csv</resultsFileFormat> </configuration> -->  

  70.                     </execution>  

  71.                 </executions>  

  72.             </plugin>  

  73.             <!-- <plugin> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_jdbc</artifactId>   

  74.                 <version>2.11</version> </plugin> -->  

  75.                   

  76.             <plugin>   

  77.               <artifactid>maven-compiler-plugin</artifactid>   

  78.               <configuration>   

  79.                   <source>1.6   

  80.                   <target>1.6</target>   

  81.                   <encoding>UTF-8</encoding>   

  82.                   <compilerarguments>   

  83.                    <extdirs>src estjmeterlib</extdirs>   

  84.                  </compilerarguments>   

  85.               </configuration>   

  86.             </plugin>   

  87.                   

  88.             <plugin>  

  89.                 <groupid>org.codehaus.mojo</groupid>  

  90.                 <artifactid>xml-maven-plugin</artifactid>  

  91.                 <version>1.0-beta-3</version>  

  92.                 <executions>  

  93.                     <execution>  

  94.                         <phase>verify</phase>  

  95.                         <goals>  

  96.                             <goal>transform</goal>  

  97.                         </goals>  

  98.                     </execution>  

  99.                 </executions>  

  100.                 <configuration>  

  101.                     <transformationsets>  

  102.                         <transformationset>  

  103.                             <dir>${jmeter.result.jtl.dir}</dir>  

  104.                             <stylesheet>src est esourcesjmeter-results-detail-report_21.xsl</stylesheet>  

  105.                             <outputdir>${jmeter.result.html.dir}</outputdir>  

  106.                             <filemappers>  

  107.                                 <filemapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">  

  108.                                     <targetextension>html</targetextension>  

  109.                                 </filemapper>  

  110.                             </filemappers>  

  111.                         </transformationset>  

  112.                         <transformationset>  

  113.                             <dir>${jmeter.result.jtl.dir}</dir>  

  114.                             <stylesheet>src est esourcesjmeter.results.shanhe.me.xsl</stylesheet>  

  115.                             <outputdir>${jmeter.result.html.dir1}</outputdir>  

  116.                             <filemappers>  

  117.                                 <filemapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">  

  118.                                     <targetextension>html</targetextension>  

  119.                                 </filemapper>  

  120.                             </filemappers>  

  121.                         </transformationset>  

  122.                     </transformationsets>  

  123.   

  124.                 </configuration>  

  125.             </plugin>  

  126.             <plugin>  

  127.                 <groupid>org.apache.maven.plugins</groupid>  

  128.                 <artifactid>maven-dependency-plugin</artifactid>  

  129.                 <executions>  

  130.                     <execution>  

  131.                         <id>copy-dependencies</id>  

  132.                         <phase>prepare-package</phase>  

  133.                         <goals>  

  134.                             <goal>copy-dependencies</goal>  

  135.                         </goals>  

  136.                         <configuration>  

  137.                             <outputdirectory>${project.build.directory}/jmeter/lib</outputdirectory>  

  138.                             <overwritereleases>false</overwritereleases>  

  139.                             <overwritesnapshots>false</overwritesnapshots>  

  140.                             <overwriteifnewer>true</overwriteifnewer>  

  141.                         </configuration>  

  142.                     </execution>  

  143.                 </executions>  

  144.             </plugin>  

  145.             <plugin>  

  146.                 <groupid>org.apache.maven.plugins</groupid>  

  147.                 <artifactid>maven-jar-plugin</artifactid>  

  148.                 <configuration>  

  149.                     <archive>  

  150.                         <manifest>  

  151.                             <addclasspath>true</addclasspath>  

  152.                             <classpathprefix>lib/</classpathprefix>  

  153.                             <mainclass>theMainClass</mainclass>  

  154.                         </manifest>  

  155.                     </archive>  

  156.                 </configuration>  

  157.             </plugin>  

  158.         </plugins>  

  159.         <defaultgoal>clean</defaultgoal>  

  160.     </build>  

  161. </project>  

4,调试工程,确保工程能正常运行

三,安装jenkins跟一些必要的插件

1,下载对应windows安装的jenkins

 

2,安装好jenkins后本地启动jenkins,进入插件管理界面,国内由于被墙了,所以需要配置一下。系统管理->管理插件->

http://localhost:8080/restart重启,再次进入插件管理界面这时可选插件界面就会有内容了

 

3,需要安装的插件

Maven Integration plugin        Maven插件

Performance plugin 性能报告插件

HTML Publisher plugin HTTP report插件

Git plugin GIT 插件

 

4,插件安装好后,就可以新建一个项目了,这里我们选择构建一个maven项目,名称为bluemoon

Jenkins+maven+jmeter接口可持续集成自动化测试


Jenkins+maven+jmeter接口可持续集成自动化测试


Build选项中输入pom.xml文件路径

Jenkins+maven+jmeter接口可持续集成自动化测试


添加构建后操作,增加一个Publish HTML reportshtml directory to archive为转译后html文件的保存路径,

index pages 为转译后的文件名称,report title jenkins中显示的标题

Jenkins+maven+jmeter接口可持续集成自动化测试


构建后操作还可以添加邮件服务模块,就是构建成功后会发送邮件通知,这里就不举例了。

 

5,点击应用,保存。这时就可以点击立即构建来触发构建功能了

Jenkins+maven+jmeter接口可持续集成自动化测试


6,Console putput可以查看输入日志,方便构建时查看哪里出现的问题

Jenkins+maven+jmeter接口可持续集成自动化测试


7,构建成功后,我们就可以查看脚本运行后的结果,点击performacefunction来查看

Jenkins+maven+jmeter接口可持续集成自动化测试


这里可以看到生成的htmljenkins使用html publisher查看报告时,发现显示不美观,不全的现象,很多东西显示不了

在查看官方文档后,这原来是安全问题所导致的。

Jenkins安全默认是将以下功能都关闭了

1javascript2html上的内置插件3、内置css或从其它站的css4、从其它站的图处5AJAX

我的网页使用的是css,所以显示不全。

解决这个问题可以在jenkins系统管理中输入以下脚本运行,就可以解决这个问题了

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

Jenkins+maven+jmeter接口可持续集成自动化测试Jenkins+maven+jmeter接口可持续集成自动化测试


四,最终效果

Jenkins+maven+jmeter接口可持续集成自动化测试Jenkins+maven+jmeter接口可持续集成自动化测试


五,遇到的其他一些问题


    1、当我们jmeter脚本中要运用jdbc来连接数据库时,此时是需要jdbc驱动的,这里解决的办法就是在dependecy中增

加依赖,然后再把这个依赖包拷贝到targetlib库中,这样用于引用jdbcjar包就放到了lib中,也就解决了找不到jdbc

驱动的问题


[plain] view plain copy

  1. <dependency>  

  2.             <groupid>mysql</groupid>  

  3.             <artifactid>mysql-connector-java</artifactid>  

  4.             <version>5.1.13</version>  

  5. </dependency>  

  6.     <plugin>  

  7.                 <groupid>org.apache.maven.plugins</groupid>  

  8.                 <artifactid>maven-dependency-plugin</artifactid>  

  9.                 <executions>  

  10.                     <execution>  

  11.                         <id>copy-dependencies</id>  

  12.                         <phase>prepare-package</phase>  

  13.                         <goals>  

  14.                             <goal>copy-dependencies</goal>  

  15.                         </goals>  

  16.                         <configuration>  

  17.                             <outputdirectory>${project.build.directory}/jmeter/lib</outputdirectory>  

  18.                             <overwritereleases>false</overwritereleases>  

  19.                             <overwritesnapshots>false</overwritesnapshots>  

  20.                             <overwriteifnewer>true</overwriteifnewer>  

  21.                         </configuration>  

  22.                     </execution>  

  23.                 </executions>  

  24.             </plugin>  

  25.             <plugin>  

  26.                 <groupid>org.apache.maven.plugins</groupid>  

  27.                 <artifactid>maven-jar-plugin</artifactid>  

  28.                 <configuration>  

  29.                     <archive>  

  30.                         <manifest>  

  31.                             <addclasspath>true</addclasspath>  

  32.                             <classpathprefix>lib/</classpathprefix>  

  33.                             <mainclass>theMainClass</mainclass>  

  34.                         </manifest>  

  35.                     </archive>  

  36.                 </configuration>  

  37.             </plugin>  

2、如下图中,报告中有写图标是没有显示出来的,造成这个问题的原因是我们用jenkins持续集成时把把代码拷到target工程中没有把相应的图标拷贝过去,那么我们只

要把这个图标拷贝过去不就可以解决了吗?答案是肯定的。

这里我在pom文件中添加了一个插件,代码如下:

[plain] view plain copy

  1. <plugin>  

  2.             <artifactid>maven-resources-plugin</artifactid>  

  3.             <executions>  

  4.                 <execution>  

  5.                     <id>copy-resources</id>  

  6.                     <phase>compile</phase>  

  7.                     <goals>  

  8.                         <goal>copy-resources</goal>  

  9.                     </goals>  

  10.                     <configuration>  

  11.                         <outputdirectory>${project.build.directory}/jmeter/html</outputdirectory>  

  12.                         <resources>  

  13.                             <resource>  

  14.                                 <!-- basedir标识所有工程 -->  

  15.                                 <directory>${basedir}/src/test/resources</directory>  

  16.                                 <filtering>true</filtering>  

  17.                             </resource>  

  18.                         </resources>  

  19.                     </configuration>  

  20.                 </execution>  

  21.             </executions>  

  22.         </plugin>  




以上是关于Jenkins+maven+jmeter接口可持续集成自动化测试的主要内容,如果未能解决你的问题,请参考以下文章

Jmeter+Maven+Jenkins+Git接口自动化流程

Jenkins+Ant+Jmeter接口自动化测试(持续构建)

jenkins+ant+jmeter接口自动化测试(持续构建)

搭建持续集成接口测试平台(jenkins+ant+jmeter)

jenkins / ant / jmeter 持续集成接口自动化

Jmeter+jenkins如何快速搭建接口和性能测试持续集成解决方案-[基于windows篇]