Jmeter + Maven + Jenkins + Git workflow automation interfaces

 

 

 

 

   In recent realization Jmeter + Maven + Jenkins + Git automation interfaces, study less than two weeks to achieve a Jmeter + Maven + Jenkins + Git whole process interface automation. Under this procedure only a simple blog to record their implementation.

I. responsibilities, a clear role

  Jmeter: performer, recording interface test script, running script, get the results, generate reports, statistical data;

  Maven, Git: managers, Maven dependency management is responsible for the project, Git code is responsible for project management;

  Jenkins: scheduler, continuous integration (CI) tools; build, deploy automation; can continue to compile and run the code; UT or run integration tests; operating results will be sent to the e-mail, or display into a report.

II. Interface Automation significance

  Basically, there are several benefits such: to provide testing efficiency; complete manual testing complex, automated testing very simple thing; repeated; improve maintenance of test coverage; routine inspection;

III. Environmental preparation

      All you want to achieve, you first need to make sure that the environment is ok. jdk, jmeter, maven, git, jenkins is ok need of.

3.1 java environment:. Java package installation, configuration environment variable

cmd enter java -version, display java version that is installed successfully

Download Java connection: https: //www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html

3.2 Jmeter installation

  Jmeter Download: http: //jmeter.apache.org/download_jmeter.cgi

  After jmeter download the compressed package, extract, configure the environment variables.

  It can be used to verify cmd, input jmeter -v, display jmeter version, jmeter can be used normally.

3.3 Maven

  maven Download link: http: //maven.apache.org/download.cgi

  Select binary zip archive link to download unpack, install;

  Configuration environment variable, MAVEN_HOME; add% MAVEN_HOME% \ bin next to the Path variable

  Verify: cmd enter mvn -v

maven run, you need to install maven IDE plug --eclipse: download and install link: https://www.runoob.com/eclipse/eclipse-install.html, install neon version. (Specific installation, not described in detail)

3.4 git 

  git Download: http://git-scm.com/downloads

  Git after the installation is complete, the menu git-git bash, click to display something similar to a command line window, indicating git installed successfully.

After which git installed, an ssh connection is required to use. Slightly, git the ssh authentication alone write.

3.5 Jenkins, if the company has operation and maintenance, general operation and maintenance Jenkins installed on the server, and then to the relevant personnel company jenkins address link, and each person's user name and password. 

Of course, there is no operation and maintenance, on your own computer can also be installed locally jenkins, build automation.

Benpian enter the following topic: the overall process automation interfaces. . ----- key automated processes

First, the use jmeter interface script ready, the script runs through, save the script jmx, export.

Second, the use eclipse open eclipse Maven project --file --- New - other,

Select a wizard, select Maven - Maven project, click next.

Interface into the New Maven Project, select the second: User default Workspace location: Use local default workspace

 

 Defaults to the first two, click next. Interface into the New Maven project, Catalog, Filter selection Artifact Id: maven-archetype-webapp, version 1.0, click next,

其中Group Id:组Id,针对一个项目的普遍唯一识别符。是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构,相当于我们日常使用的包名,例如:org.hhh,最好是填写公司或者组织名称 com.company Artifact Id:要新建的项目的名字,项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称 Version:版本号,默认0.0.1-SNAPSHOT。 package:输入group、artifact,会自动生成package,目录+名字=完整路径。输入group,artifact,点击next,生成maven项目 注意:group id+artifact id+version必须是一个唯一的名字,不要和任何别的jar包全名一模一样,否则会引发冲突。

 

 使用eclipse,创建maven项目、成功。

使用eclipse 创建maven项目,只会自动生成src/main/resources 和target/classes, target/test-classes,需要自己手动添加其他目录,并且指定项目的source以及编译后的输出目录。 使用eclipse运行接口测试脚本,选择project 右击----run as ------maven build,然后再弹出的对话框Goals 写上verify,--run。运行后会有测试结果文件,显示BUILD SUCCESS,即为运行成功。

二)、Maven项目约定

Maven中使用项目约定,它约定Java源代码放在哪个目录,约定编译的class文件放在哪个目录,约定资源或配置文件放在哪个目录,每一种文件的存放目录都有着明确的约定,下面是Maven的项目约定结构图:

MavenProject(项目根目录)    

|----src —— 源代码目录    |     

  |----main        ----  开发使用

    |----java —— 存放项目的.java文件     

    |----resources —— 存放项目资源文件,如spring、hibernate配置文件    |      |       

    |----webapp ---如果是web项目则需要该目录,存放WEB-INF目录、web.xml、jsp、html、js等文件    | 

  |----test -----测试 主用         

    |----java —— 存放所有测试.java文件,如JUnit测试类  

    |----resources —— 存放项目测试用的资源文件,如spring, hibernate配置文件  

|----target —— 项目输出目录,存放经过编译的class文件和打包部署的jar包等    

|----pom.xml —— 定义项目的对象模型,同时标识该项目是一个Maven项目

三)、本地构建Maven项目

1.在src下手动新建test目录,src/test目录下新建jmeter文件夹,把准备好的jmeter接口测试的jmx脚本复制到这个文件夹下

2.在src/test目录下创建resources文件夹,并把测试模板(E:\apache-jmeter-5.1\apache-jmeter-5.1\extras的如下文件)复制到这个resources文件下

文件1:jmeter-results-detail-report_21.xsl 

文件2:jmeter-results-report_21.xsl

3.将下面五个文件从jmeter/bin目录复制到 src/test/jmeter文件夹

文件1 :jmeter.properties

文件2:saveservice.properties

文件3:system.properties

文件4:upgrade.properties

文件5:user.properties

四)、关键点:pom.xml 配置文件 -------构建 Maven项目中最重要的一个文件

pom 配置文件,maven项目生成时 ,pom文件中是有一部分内容的。 我们主要是在pom 文件中增加各种插件

4.1 测试报告---html地址配置。properties 增加在dependencies依赖之前

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<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.html.dir1>${project.build.directory}/jmeter/html1</jmeter.result.html.dir1>

<ReportName>TestReport</ReportName>

</properties>

其中需要注意,多个报告时, html 与 html1, dir 与dir1。 如果设置一样,生成报告 可能会存在 报告一致的情况。

其他插件增加在 dependencies依赖 build finalName 后

测试报告configuration中的 transformationSets配置

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

<stylesheet>src/test/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>

核心插件jmeter -maven-plugin     --------核心插件

<plugin>

<groupId>com.lazerycode.jmeter</groupId>

<artifactId>jmeter-maven-plugin</artifactId>

<version>2.4.1</version>

<configuration>

<resultsFileFormat>xml</resultsFileFormat>

<ignoreResultFailures>true</ignoreResultFailures>

<ignoreResultErrors>false</ignoreResultErrors>

<testResultsTimestamp>false</testResultsTimestamp>

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

</configuration>

<executions>

<execution> <id>jmeter-tests</id>

<phase>verify</phase>

</plugin>

<goals>

<goal>jmeter</goal>

</goals>

</execution>

</executions>

此核心插件:用来执行jmx脚本,版本号特别注意。 上一页与本页是 该核心插件。其中配置解释 resultsFileFormat:设置jmeter生成结果文件格式; ignoreResultFailures:设置忽略失败是否停止运行; ignoreResultErrors:设置忽略错误是否停止运行 testResultsTimestamp:设置结果是否有时间戳 resultsDirectory: 指定jtl 生成目录。

脚本所在文件夹

<executions>

<execution>

<id>jmeter-tests</id>

<phase>verify</phase>

<goals>

<goal>jmeter</goal>

</goals>

</execution>

</executions>

插件 xml-maven-plugin, 配置文件中: (该插件:根据xsl模板把jtl文件转换成html) ​

<groupId>org.codehaus.mojo</groupId>

<artifactId>xml-maven-plugin</artifactId>

<version>1.0.2</version>

<executions>

<execution>

<phase>verify</phase>

<goals>

<goal>transform</goal>

</goals>

</execution>

</executions>

<configuration>

xml 插件的configuration 中配置两个报告 transformationSet ,每个报告中包括dir,stylesheet,outputDir,报告配置如下

<transformationSet>

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

<stylesheet>src/test/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>

注意:多个报告时,报告的stylesheet、outputDir 必须不能一致。stylesteet 是指定的报告样式,可以自己网上下载样式,放置jmeter 相关文件中,这里写上;也可以使用jmeter 包中现有的 报告样式; 另外,jmeter的配置文件,其实可以进行修改 代码,来获得自己想要的报告样式;

最后一个插件,XSLT插件  (忘记这个插件是什么作用了)

<dependency>

<groupId>net.sf.saxon</groupId>

<artifactId>saxon</artifactId>

<version>8.7</version>

</dependency>

基本以上,是所有的pom 中需要配置的插件。 代码需要自己输入,粘贴会出现 缩进等问题

五)、eclipse运行接口测试脚本

运行接口测试脚本

选择project 右击----run as ------maven build,然后再弹出的对话框Goals 写上verify,--run。

运行后会有测试结果文件。运行,console中等待运行完毕,显示build success,即运行成功。

脚本构建成功,在target中生成报告,target中 jmeter/html 中是生成的报告。

如果有多维度报告,jmeter会存在多个html。

注意问题:如果需要生成多个报告,pom.xml文件中html的transformationSet 中,stylesheet 配置两个报告模板,生成html报表文件路径outputDir 不能重复,一个dir,一个dir1 到此,jmeter + maven项目,本地已经构建成功。可成功生成报告。

 

 

 

 三、Jenkins   本地下载安装

https://jenkins.io/download/ 官网下载安装;

坑一:jenkins中新建项目 没有一个 “创建一个Maven项目”的选项

解决:需要安装Maven 项目插件:Maven Integration.

系统管理---管理插件--可选插件,搜索框中输入插件名Maven Integration,搜索到,点击安装,安装完毕重启,重启ok。

jenkins 构建maven本地项目

Maven项目本地调试通过,在cmd中切换到maven 项目 的pom.xml 所在路径, 执行mvn clean install ,显示BUILD SUCCESS----执行成功

jenkins中 Manage Jenkins, Glogal Tool Configuration, 配置jdk,

其中,install automatically ,如果想使用本地jdk,可以不勾选。

写上本地jdk 版本号,jdk 本地安装路径 maven 使用本地maven的话,同样不勾选,install automatically ,写上本地maven 版本号,本地maven 安装路径。

配置完成,点击应用。 (本地jenkins,JDK,maven需要自己配置。 公司服务器,jenkins 基础服务运维负责)

 

 Jenkins 本地构建

jenkins新建项目----maven项目,构建Maven项目,

General:输入简单的描述               源码管理:None   

Pre Steps:Execute Windows batch command

命令:  (cmd 命令窗口框中的命令一致,即可。)

cd C:\Maven\MavenProject\testOne    

mvn clean install

Build

Root POM: C:\Maven\MavenProject\testOne\pom.xml

点击:应用。

即可 应用,构建的时候,起作用。

点击构建,点击build 下的本次构建的进度条,--console output,查看日志,日志最后Finish success,即构建成功。 构建失败,同样可以在日志中查看错误。

二)、Jenkins Html测试报告

系统管理---- 插件管理-- 可选插件,下载HTML Publisher。 (本地jenkins需要,公司jenkins,运维)

构建项目,构建后操作-----Publish HTML reports 构建后操作-----Publish HTML reports,

HTML directory to archive: target\jmeter\html (jenkins本地项目可使用路径,一般连接公司jenkins时,默认所在位置:当前项目目录,所以直接输入当前目录后路径即可)

index page:index.html

Report title: 测试报告

三)Jenkins 邮件通知

Email Extension Plugin 插件

系统管理---系统设置,在系统设置中找到Jenkins Locaction 填好JenkinsURL 跟系统管理员的邮件地址,注意必填。

邮件通知--点击高级配置 QQ邮箱smtp服务器地址:**smtp.qq.com** (qq邮箱要登陆邮箱→设置→账户 开启SMTP服务) 使用SMTP认证,勾选。

jenkins 邮件通知输入,

用户名:输入自己的QQ

密码:Q邮箱的话,是你开启smtp服务后他会给你密码的,而不是你的QQ邮箱密码了。

端口号:465 勾选通发送测试邮件测试配置 ,Test configuration 点击,测试是否配置成功。 如果email 显示成功,则测试 邮件发送成功。

注意:邮件端口号:一定 465,其他,邮件通知发送不成功。

四、 Jmeter + Maven + Jenkins + Git

本地代码放置git上,代码同步;

源码管理,选择Git,输入git 上该项目http地址,输入git用户名、密码

需要配置--general、源码管理、构建触发器、Build、Post Steps、构建设置(E-mail Notification)、 构建后操作(html报告,邮件Edirtable Email Notification)--- 搞定。

使用 git中代码,Pre Steps 就不需要输入命令。 ---build 中 pom + goals 命令就可以了 (使用公司服务jenkins,一般jenkins各种系统配置运维会配置。个人拿到自己的账号、密码,登录成功,就可以构建自己的自动化项目了)

Build中,root Pom 输入maven 项目 ./pom.xml Goals and options :clean verify

Post Steps   

构建三个选择---可根据需要设置

 

 

构建设置:邮箱设置,多个邮箱,英文空格为间隔。选择第一、第三 ------(可根据自己实际需要进行设置) 每一次不稳定的构建、每一次失败的构建--都需发送邮件

 

 

构建后操作 构建后 HTML 报告,报告路径:target/jmeter/html index page:多个jmx 脚本,多个报告时,以英文逗号隔开 Report title:jenkins运行完毕脚本后,显示的报告title

 

构建后邮件设置

构建后邮件-Editable Email Notification

勾选 Project Recipient List :[email protected] [email protected] (邮件接收人,多个接收人时可空格/逗号 隔开)

Project Reply-To List : $DEFAULT_REPLYTO

Content Type :HTML(text/html) ---- 邮件 中内容格式

Default Subject : $DEFAULT_SUBJECT

Default Content : $DEFAULT_CONTENT

Advanced Setting:点击 (高级配置)

Pre-send Script :$DEFAULT_PRESEND_SCRIPT

Post-send Script: $DEFAULT_POSTSEND_SCRIPT

Triggers , 勾选 Always, send to --勾选 Recipient List, 高级--点击

展开中: Recipient List :收件人

Reply-To List: $PROJECT_DEFAULT_REPLYTO (回复列表)

Content Type:HTML(text/html)

Subject: 【接口测试报告】$PROJECT_NAME - $BUILD_STATUS - Build #$BUILD_NUMBER!

subject--邮件标题

Content:邮件中内容,是一段 html 代码。

点击应用。。

到目前为止,jmeter + maven +jenkins +git,项目已全部增加。

jenkins中点击testone,进入;

点击左侧--立即构建,下面显示构建进度条 进度条走完,变成 蓝色球:构建成功;

变成灰色球,构建失败;

构建过程中,点击 该次构建,进入本次构建详情页, 构建详情页中,左侧有控制台输出,点击进入可查看 构建日志 构建完毕,收件人邮箱收到 本次构建结果。

 

Guess you like

Origin www.cnblogs.com/MTXue/p/11209268.html