JMeter+Ant+jenkins builds an interface automation test environment

Table of contents

Foreword:

       1. Introduction to ant

  2. Build ant environment

  3. JMeter and Ant integration

  4. Report Optimization

  5. Jenkins continuous integration


Foreword:

JMeter is an open source performance testing tool that can be used to test the performance of web applications or API interfaces, and supports multiple communication protocols and data formats. Ant is a build tool that can be used to automate the building, testing, packaging, and deployment of software projects. Jenkins is an automated build tool that supports an integrated development environment.

1. Introduction to ant

  1.1 What is ant?

  JMeter: Write test scripts to generate jmx scripts and run interface tests. JMeter is equivalent to a basketball court, and jmx script is equivalent to a basketball running field.

  ant: A build tool that runs test scripts in batches by calling JMeter and generates test reports. Ant is a powerful package compilation tool. We use it to convert JTL (xml) format files into html format files, which is equivalent to the role of a basketball coach.

  1.2 What is a build

  The concept can be found everywhere. Visually speaking, you need to take the code from a certain place, compile it, copy it to a certain place, and so on. Of course, it is not only this, but it is mainly used to do this.

  2. Build ant environment

  1) Go to the Apache official website to download the latest version of ant. I downloaded apache-ant-1.9.15.

  2) Unzip it after downloading and put it under a directory. I put it and jmeter under D:\mysolution.

  3) Configure environment variables, similar to jmeter. The following are related to the path where your ant is stored.

  Note: It is said on the Internet that ANT_HOME, CLASSPATH and Path need to be set. For personal testing, you can only set Path and the other two items can not be set.

  New system variable ANT_HOME, value D:\mysolution\apache-ant-1.9.15

  System variable CLASSPATH, add D:\mysolution\apache-ant-1.9.15\lib after the value

  System variable Path, add D:\mysolution\apache-ant-1.9.15\bin after the value

  4) Enter the command prompt and enter ant. The result is shown in the figure below. "build.xml does not exist" indicates that the ant configuration is successful.

  3. JMeter and Ant integration

  3.1 JMeter section

  1) Copy the ant-jmeter-1.1.1.jar jar package in the D:\mysolution\apache-jmeter-5.3\extras folder to the D:\mysolution\apache-ant-1.9.15\lib folder .

  2) Open jmeter\bin\jmeter.properties with notepad, change jmeter.save.saveservice.output_format=csv to jmeter.save.saveservice.output_format=xml, remember to remove the front "#".

  3) Set the output content and modify the jmeter.properties file.

  Note: You can try to understand the meaning of each line of configuration, and then perform the personalized configuration you want.

  4) Create a build.xml file in the JMeter script directory, please configure it according to the actual situation.

  Note: Don't look for the build.xml file, just create a text file and copy the following content. It is enough to modify the corresponding place of the comment during configuration.

<?xml version="1.0" encoding="UTF8"?>
<project name="ant-jmeter-test" default="run" basedir=".">
    <!-- 需要改成自己本地的 Jmeter 目录-->  
    <property name="jmeter.home" value="D:\mysolution\apache-jmeter-5.3" />
    <property name="report.title" value="JMeter接口测试报告"/>
    <!-- jmeter生成jtl格式的结果报告的路径--> 
    <property name="jmeter.result.jtl.dir" value="D:\Jmeter\report\jtl" />
    <!-- jmeter生成html格式的结果报告的路径-->
    <property name="jmeter.result.html.dir" value="D:\Jmeter\report\html" />
    <!-- 生成的报告的前缀-->  
    <property name="ReportName" value="APITestReport" />
<tstamp> <format property="time" pattern="YYYYMMDD HHmmss" /></tstamp>
    <property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${ReportName} ${time}.jtl" />
    <property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${ReportName} ${time}.html" />
    <target name="run">
        <antcall target="test" />
        <antcall target="report" />
    </target>
    
    <target name="test">
        <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
        <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
            <!-- 声明要运行的脚本"*.jmx"指包含此目录下的所有jmeter脚本-->
            <testplans dir="D:\Jmeter\script" includes="*.jmx" />
            
            <property name="jmeter.save.saveservice.output_format" value="xml"/>
        </jmeter>
    </target>
        
    <path id="xslt.classpath">
        <fileset dir="${jmeter.home}/lib" includes="xalan*.jar"/>
        <fileset dir="${jmeter.home}/lib" includes="serializer*.jar"/>
    </path>
    <target name="report">
        <tstamp> <format property="report.datestamp" pattern="YYYY/MM/DD HH:mm" /></tstamp>
        <xslt 
              classpathref="xslt.classpath"
              force="true"
              in="${jmeter.result.jtlName}"
              out="${jmeter.result.htmlName}"
              style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl">
              <param name="dateReport" expression="${report.datestamp}"/>
              <param name="titleReport" expression="${report.title}:${report.datestamp}"/>
       </xslt>
                <!-- 因为上面生成报告的时候,不会将相关的图片也一起拷贝至目标目录,所以需要手动拷贝 --> 
        <copy todir="${jmeter.result.html.dir}">
            <fileset dir="${jmeter.home}/extras">
                <include name="collapse.png" />
                <include name="expand.png" />
            </fileset>
        </copy>
    </target>
</project>

  3.2 ant part

  Run ant, go to the directory where build.xml is located, and run ant. Note You can also use ant -buildfile build.xml to run ant.

  After running, if there is a SUCCESS message, it means that the running is successful. You can go to the corresponding directory to view the results. Part of the running results information is as follows:

BUILD SUCCESSFUL
Total time: 9 seconds

  3.3 View JMeter results

  This report is a report generated in the style of jmeter-results-detail-report_21.xsl in the \apache-jmeter-5.3\extras directory.

  4. Report Optimization

  Ant's default report is not good-looking. After an error occurs, it is difficult to locate the problem from the report. Where the problem is located, it can be optimized. Optimization principle: In fact, the style file plays a key role in the process of converting the report from .jtl format to .html format.

  1) Download the style file: jmeter.results.shanhe.me.xsl

  2) Put the downloaded file in the extras directory of jmeter.

  3) Modify the following part of the build.xml file, just modify the file name of the xsl style file.

  4) Running results:

  5. Jenkins continuous integration

  Jenkins is an open source continuous integration (CI) tool that provides a friendly operation interface. It originated from Hudson (Hudson is commercially available). It is mainly used for continuous and automatic construction/testing of software projects and monitoring the operation of external tasks (this is more abstract. , write it down for the time being, without explanation). Written in the Java language, Jenkins can run in popular servlet containers like Tomcat or stand alone. Usually used in conjunction with version management tools (SCM), build tools. Commonly used version control tools include SVN, GIT, and build tools include Maven, Ant, and Gradle.

  In short: Jenkins is to realize the scheduled operation of continuous integration platform scripts, email sending, test report display, etc.

  5.1 Installation and configuration of jenkins

  Jenkins download address: https://jenkins.io/zh/download/, download the windows version and install jenkins. After the installation is successful, it will automatically open: http://localhost:8080 page, waiting to enter the jenkins configuration page.

  Unlock jenkins to complete the configuration.

  Regarding the choice of plug-ins, it is still an old routine, and it is easy to install the recommended plug-ins, and the experts can customize them.

  Create an admin user.

  Instance configuration.

  5.2 Use of jenkins

  5.2.1 Create a new job

  5.2.2 Add Ant plug-in in jenkin configuration

  Add the path of the file to be built, that is, the path of the build.xml file executed by Ant.

  5.2.3 Build Now

  5.2.4 Build triggers

  Schedule Description: There are 5 parameters in it

  ·The first one is for minute H means random

  ·The second is every 4 hours from 9:00 to 3:00 pm on behalf of hours 9-15/4

  ·The third represents the day* Any day

  ·The fourth represents the month 1-11, representing the month from January to November

  ·The fifth one represents week 1-5 and represents working days

  ·H(9-18) A random point from 9 o'clock to 18 o'clock, if H is not used for randomness, do not add brackets

  Common schedule configuration table:

  * * * * * Indicates that any time period may trigger execution at the same time. Not recommended for use.

  H/30 * * * * means build every 30 minutes every day.

  H 4-19/3 * * * indicates that the build is every 3 hours from 1:00 am to 7:00 pm.

  * * 3-5 * * means that the construction will be done on the 3rd, 4th, and 5th of each month, and the specific time is unknown.

  * * * * 1-5 means that it will be built on weekdays, and the specific time is unknown.

  H/30 8-19/3 1-28 1-11 1-5 means from January to November 1st to 28th, every working day, every 3.5 hours from 8 am to 7 pm will trigger a build.

  5.3 Other configurations of jenkins

  5.3.1 Discard old configuration

  Server resources are limited, and sometimes too many historical builds are saved, which will slow down Jenkins, and the server hard disk resources will also be full. Of course, the "Number of days to keep building" and the maximum number of keeping builds below can be customized, and a reasonable value needs to be determined according to the actual situation.

  5.3.2 Mail Notification Settings

  1) Return to the outermost main interface. Find manage jenkins and find system settings.

  2) Find the mail plugin in the plugin installation here.

  3) Check the installed email plugins.

  4) Return to the configuration interface, then click System Configuration, find the place in the picture below: fill in two things:

  The first url: use the default url to try to access jenkins; the second parameter: the account of the mailbox outbox.

  5) Go to the mailbox configuration at the bottom, click Advanced, and fill in the content as required. . Please pay attention to the password: it is the authorization code of the QQ mailbox here, and the user name should not be followed by the suffix. Then check the test, and you will receive an email after success.

  SMTP basic settings, see the figure below:

  Extended E-mail Notification:

  This is the mailbox function using a third-party plug-in, the method is the same as the one above.

  5.4 Construction result description

  5.4.1 Build Status

  Successful blue: The build is complete and is considered stable.

  Unstable Yellow: The build is complete but is considered unstable.

  Failed red: The build failed.

  Disable gray: the build is disabled.

  5.4.2 Build stability

  Build stability is represented by weather: sunny, sunny to cloudy, cloudy, light rain, thunderstorm. Better weather means a more stable build, and vice versa.

  5.4.3 Build history interface

  console output: Output the log information of the build.

 As someone who has been here, I also hope that everyone will avoid some detours, and I hope it can help you. (WEB automated testing, app automated testing, interface automated testing, continuous integration, automated test development, big factory interview questions, resume templates, etc.), I believe it can make you better progress! 

Just leave [Automated Test]

Guess you like

Origin blog.csdn.net/Free355/article/details/131187680