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的调用等,下个笔记见。