jmeter+maven+jenkins自動化接口測試(上)


代碼已上傳git(包括調試的jmx,jmeter相關文件等):https://gitlab.com/yinzhenzhi/jmeterandmaven

目的:現在很多人都在做自動化接口的平台,我也正在學,不過有更方便的方法避免了重復造輪子,試試也可以的。

工具解釋:jmeter是使用Java語言編寫的成熟的性能測試和接口測試工具,maven是java的構建工具,對於jar包的引入非常方便(也有用ant工具的,不過我沒用過),jenkins是持續集成工具,主要是做一些定時任務的執行。通過這三種工具的結合,可以通過jenkins工具來做定時的接口測試並發送測試報告,是一種比較方便而且省力的方法,不過和現在比較成熟的用代碼寫的報告相比也有一定的缺陷性。

工具版本:maven--3.5.0 ,jmeter-3.3.0 ,jenkins--最新版本(截至2018-10-24)

前提:已安裝jenkins和maven工具,已下載jmeter3.3的包並解壓(最新的已經是5.0,用這個版本也可以,只不過對應的maven-jmeter-plugin也需要更新到2.8.0版本,我沒試過)。

步驟:

1.eclipse創建maven項目

2.在項目中創建文件夾如下,其中的文件,除了jmx是你要執行的jmeter腳本,其他的都是jmeter的安裝文件里面的配置信息和圖片報告模板之類的:

3.編輯pom.xml,然后執行 run as--maven build(goal輸入verify)

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>testMavenandJmeter</groupId>
  <artifactId>zhizhi</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>zhizhi</name>
  <url>http://maven.apache.org</url>
 <properties>
        <jmeter.result.jtl.dir>${project.build.directory}\jmeter\results</jmeter.result.jtl.dir>
        <jmeter.result.html.dir>${project.build.directory}\jmeter\html</jmeter.result.html.dir>
        <jmeter.result.resource.dir>${project.build.directory}\jmeter\html1</jmeter.result.resource.dir>
        <jmeter.result.html2.dir>${project.build.directory}\jmeter\html2</jmeter.result.html2.dir>
        <ReportName>TestReport</ReportName>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>

        <!--此處functions、json、postgresql為自己引入的jar包 -->
        <!-- 
        <dependency>
            <groupId>com.functions.jmeter</groupId>
            <artifactId>functions</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.json.jmeter</groupId>
            <artifactId>json</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
          <groupId>com.postgresql.jdbc</groupId>
          <artifactId>postgresql</artifactId>
          <version>1.0.2</version>
        </dependency>
         -->
    </dependencies>
   
    <build>
    	<finalName>AutoTest</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <artifactId>mysql-connector-java</artifactId>
                    <outputDirectory>${project.build.directory}/jmeter/lib</outputDirectory>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.lazerycode.jmeter</groupId>
                <artifactId>jmeter-maven-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <resultsFileFormat>xml</resultsFileFormat>
                    <ignoreResultFailures>true</ignoreResultFailures>
                    <testResultsTimestamp>false</testResultsTimestamp>
                </configuration>
                <executions>
                    <execution>
                        <id>jmeter-tests</id>
                        <goals>
                            <goal>jmeter</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${jmeter.result.html.dir}</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}/src/main/resources</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>xml-maven-plugin</artifactId>
                <version>1.0.1</version>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>transform</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <transformationSets>
                        <transformationSet>
                            <dir>${jmeter.result.jtl.dir}</dir>
                            <stylesheet>src/main/resources/jmeter-results-detail-report_21.xsl</stylesheet>
                            <outputDir>${jmeter.result.html.dir}</outputDir>
                            <fileMappers>
                                <fileMapper
                                        implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
                                    <targetExtension>html</targetExtension>
                                </fileMapper>
                            </fileMappers>
                        </transformationSet>
                    </transformationSets>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>net.sf.saxon</groupId>
                        <artifactId>saxon</artifactId>
                        <version>8.7</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

  

以上代碼的全部已上傳在git,自己調不出來的直接拿來主義,把jmx換掉就可以了。請務必注意,我的jmeter-maven-plugin版本是2.4.1,這個版本可能不兼容jmeter4.0和5.0,如果是這兩個版本的jmeter的腳本,可能需要將jmeter-maven-plugin的版本更新為對應的2.7.0或2.8.0.

 

以上是maven+jmeter的結合使用,jenkins的調用等,下個筆記見。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM