一、环境搭建
1、准备一台 Linux 服务器,安装 jdk,maven,jmeter,和 Jenkins
1.1、版本为 jdk1.8.0,maven 为 3.6.3,jmeter 为 5.3,Jenkins 为 2.210
1.2、将 jdk 压缩包上传到服务器的/opt/目录下,进行解压,再修改环境变量,vim/etc/profile,新增如下内容,新增成功后再执行 source /etc/profile 使环境变量生效。然后再执行 java -version 查看是否配置成功。
PATH=$PATH:$HOME/bin
JAVA_HOME=/opt/jdk1.8.0_201
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=:.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH
1.3、将 maven 压缩包上传到服务器的/opt/目录下,进行解压,在修改环境变量 vim/etc/profile,新增如下内容,新增成功后再执行 source /etc/profile 使环境变量生效。然后再执行 mvn -version 查看是否配置成功。
MAVEN_HOME=/opt/apache-maven-3.6.3
PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH
export MAVEN_HOME
1.4、将 jmeter 压缩包上传到服务器的/opt/目录下,进行解压。
1.5、下载 Jenkins,类型为 rpm 文件,将文件上传到服务器的/opt/目录下,执行 rpm –ivh jenkins-2.210-1.1.noarch.rpm,在输入 service jenkins start 启动 Jenkins
二、创建 maven 项目
1、新增一个 maven 项目,在 maven 项目中的 src/test 新增两个文件夹,分别为 jmeter 和 resources,一般情况下 resource 是都是自带的,只需新增 jmeter 目录就好了。
1.1、jmeter 文件夹存放的是 jmeter 文件,将 jmeter 的 bin 目录下 jmeter.properties, saveservice.properties, system.properties, upgrade.properties, user.properties 拷贝到 jmeter 文件夹中。
1.1.1、jmeter 文件还存放 jmeter 脚本和 Python 文件,Python 文件主要用于统计脚本成功数,失败数和总数,与钉钉发送消息等功能。
1.2、将 jmeter.properties 文件中的 jmeter.save.saveservice.output_format=csv 改成 jmeter.save.saveservice.output_format=xml。
1.3、将 jmeter-results-detail-report_21.xml,jmeter-results-report_21.xml, expand.png, collapse.png 这四个文件拷到 resources 文件夹中,
1.4、添加 pom.xml 文件
<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>com.kuka.Jiasiwen</groupId>
<artifactId>Jiasiwen</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Jiasiwen</name>
<url>http://maven.apache.org</url>
<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.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>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>5.3</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>5.3</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>jorphan</artifactId>
<version>5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
</dependencies>
<build>
<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>3.1.1</version>
<configuration>
<resultsFileFormat>xml</resultsFileFormat>
<ignoreResultFailures>true</ignoreResultFailures>
<testResultsTimestamp>false</testResultsTimestamp>
<jMeterProcessJVMSettings>
<xms>1024</xms>
<xmx>1024</xmx>
<arguments>
<argument>-Xprof</argument>
<argument>-Xfuture</argument>
</arguments>
</jMeterProcessJVMSettings>
</configuration>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
<execution>
<id>configuration</id>
<goals>
<goal>configure</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</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>${project.basedir}/src/test/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/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>
</transformationSets>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>saxon</artifactId>
<version>8.7</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
test_auto.py 文件用来在钉钉群统计成功率
#coding=utf-8
import requests
import time
import json
import sys
import hashlib
from lxml import etree
HEADERS = {'ua': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36'}
DINGDING_URL= 'https://oapi.dingtalk.com/robot/send?access_token=af60bd5dcef4e4bcc281c6d20af07becae42c6086bae882e8722535d6eba2271'
RESULT_FILE= 'target/jmeter/html/test11.html'
REPORT_URL_FILE= 'http://192.168.248.124:8080/job/test_auto/HTML_20Report/'
class Message():
def __init__(self):
self.total=0;
self.passed=0;
self.failed=0;
self.reportUrl="";
def analyze(self):
try:
retail_file = open(RESULT_FILE,"r")
f = retail_file.read()
html = etree.HTML(f)
self.total = html.xpath('string(//table[2]/tr[2]/td[1]/text())')
self.passed = html.xpath('string(//table[2]/tr[2]/td[2]/text())')
self.failed = html.xpath('string(//table[2]/tr[2]/td[3])')
self.SuccessPercent = html.xpath('string(//table[2]/tr[2]/td['
'4]/text())')
#file = open(REPORT_URL_FILE,'r')
self.reportUrl=REPORT_URL_FILE
except:
print ("read file error");
def send_message_to_robot(self):
url= DINGDING_URL
message='test_auto自动化执行结果:\n本次执行了{}条用例;\n成功了{}条;\n失败了{}条;\n成功率:{' \
'};\n查看详情请点击=> ' \
'{}'.format(self.total,self.passed,self.failed,self.SuccessPercent,
self.reportUrl)
data={"msgtype":"text","text":{"content":message,"title":"用户经营自动化结果通知"}}
try:
resp = requests.post(url,headers=HEADERS,json=data,timeout=(3,60))
except:
print ("Send Message is fail!");
if __name__ == '__main__':
message = Message()
message.analyze();
message.send_message_to_robot();
效果如下:
三、
1、访问 Jenkins,http://{服务器外网 IP}:8080 进行访问,默认端口为 8080,可根据实际情况修改端口。
2.配置环境变量,在系统管理 - 全局工具配置菜单下配置环境变量,如下:
3、安装插件,在系统管理 - 插件管理 - 可选插件菜单下安装如下插件:
4、配置钉钉机器人,在系统管理 - 系统配置下配置机器人,webhook 从钉钉机器人获取
四、构建 job 任务,点击新建任务,选择 maven 项目
1、配置 job
执行 shell 命令:
/usr/bin/xsltproc --stringparam current-date "date +%Y-%m-%d\ %H:%M:%S
" -o $WORKSPACE/target/jmeter/html/test11.html $WORKSPACE/src/test/resources/jmeter-results-detail-report_21.xsl $WORKSPACE/target/jmeter/results/test11.jtl;
\cp -rf $WORKSPACE/target/jmeter/html/test11.html /var/lib/jenkins/jobs/test_auto/htmlreports/HTML_20Report/;
注:需要将 test11 文件名修改成对应的文件名,test_auto 则修改成 job 名称
测试结果: