jmeter+ant+Jenkins continuous integration automated testing

1. Environment preparation
jdk, jmeter, jenkins, ant Jmeter
download address: http://jmeter.apache.org/download_jmeter.cgi
Jenkins download address: https://www.jenkins.io/download
ant download address: http:/ /ant.apache.org/bindownload.cgi
JDK download address: htps://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
jmeter environment variable configuration
1. New system variable JMETER_HOME, The value is the installation path
Insert picture description here
2. The system variable JAVA_HOME is added, and the value is the installation path
Insert picture description here
3. The system variable ANT_HOME is added, and the value is the installation path
Insert picture description here
4. Edit the system variable CLASSPATH, and the value is added %JMETER_HOME%\lib\ext\ApacheJMeter_core.jar ;%JMETER_HOME%\lib\jorphan.jar;%JMETER_HOME%\lib\logkit-2.0.jar;.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;%ANT_HOME%\lib
Insert picture description here
5. Edit PATH, add %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;%ANT_HOME%\bin;%ANT_HOME%\bin.
Insert picture description here
2. Jmeter+ant integration
1. Download and install ant and configure its system variables.
Enter ant in the command line window -v check if the installation is successful
2. File configuration
2.1. Configure library files
Copy the ant-jmeter-1.1.1.jar file in the jmeter extras directory to the lib folder in the ant installation directory
2.2. Configure the ant compilation file build.xml (new build. xml file)
arbitrarily create a script storage directory: E:\jmeter\AutoTest, and the report storage directory report
Insert picture description here
Insert picture description here
code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<project name="JmeterTest" default="run" basedir=".">
    <tstamp>
        <format property="time" pattern="yyyyMMddhhmm" />
    </tstamp>
    <!-- 需要改成自己本地的 Jmeter 目录-->
    <property name="jmeter.home" value="D:\apache-jmeter-5.4" />
    <!-- jmeter生成jtl格式的结果报告的路径-->
    <property name="jmeter.result.jtl.dir" value="D:\jenkins\workspace\report\jtl" />
    <!-- jmeter生成html格式的结果报告的路径-->
    <property name="jmeter.result.html.dir" value="D:\jenkins\workspace\report\html" />
    <!-- 生成的报告的前缀 -->
    <property name="ReportName" value="TestReport" />
    <property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${ReportName}.jtl" />
    <property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${ReportName}.html" />
    <!-- 接收测试报告的邮箱 -->
    <property name="mail_to" value="[email protected]" />
    <!-- 电脑地址 -->
    <property name="ComputerName" value="jkqsh-l0285" />
    <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:\jenkins\workspace\接口测试" includes="*.jmx" />
        </jmeter>
    </target>
    <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.shanhe.me.xsl" />  
        <!-- 因为上面生成报告的时候,不会将相关的图片也一起拷贝至目标目录,所以,需要手动拷贝 -->
        <copy todir="${jmeter.result.html.dir}">
            <fileset dir="${jmeter.home}/extras">
                <include name="collapse.png" />
                <include name="expand.png" />
            </fileset>
        </copy>
    </target>
	<path id="xslt.classpath">
        <fileset dir="${jmeter.home}/lib" includes="xalan*.jar"/>
        <fileset dir="${jmeter.home}/lib" includes="serializer*.jar"/>
    </path>
</project>

2.3 configured jmeter.propertise document
to the next jmeter installation directory / bin directory, jmeter.propertise open and edit the document, modify the report output format xml jmeter
modified xml = jmeter.save.saveservice.output_format
. 3, configured, perform a test construct
the Save the previous jmeter script, and place the build.xml configuration file in the same directory as the test script.
CMD enters the script directory: E:\apache-jmeter-2.13\AutoTest. (That is, the directory where build.xml is located)
Enter ant Successfully built
when the first build fails and an error is reported.
Build failure report
This error is resolved: the reason is that the two jar packages of ant are not configured in the specified path and need to be added in build.xml The following code

	<path id="xslt.classpath">
        <fileset dir="${jmeter.home}/lib" includes="xalan*.jar"/>
        <fileset dir="${jmeter.home}/lib" includes="serializer*.jar"/>
    </path>

3.1. Optimize the test report
Download template: http://shanhe.me/download.php?file=jmeter.results.shanhe.me.xsl
3.1.1 Copy to the extras directory of jmeter
3.1.2 Set the test output report to be output Contents: In jmeter.properties in the bin directory of jmeter, set the content that needs to be output to true, and remove the previous comment symbol #
Insert picture description here
3.1.3 Set the report template of the build file to the optimized template jmeter-results-shanhe-me .xsl
Insert picture description here
3.1.4 Build the test with ant again, and view the optimized test report
Insert picture description here
3. Continuous integration of jmeter+ant+Jenkins
1. Official website download address: https://www.jenkins.io/download/
One-click installation and access to the local Address: http://localhost:8080/
Add a user, and click Settings on the user page, and the password will take effect after changing the password.
Install the plug-ins HTML Publisher, Ant In Workspace, Email Extension Template Insert picture description here
2. Create a new view, click OK and save
Insert picture description here
3. In Create a new task in the view.
Enter the task name and select the free-style software project, click ok
Insert picture description here
4. Configure to build
Insert picture description here
5. Configure report
This plug-in will not help you generate any html files, just copy the files generated by the corresponding project under your workspace to the corresponding Under the jobs directory
HTML directory to archive: The relative path is used. This corresponds to
Index pages in the corresponding jobs directory of the current jenkins : the name of the test report integrated in jenkins.
Report title: the name displayed under Jenkins.
Insert picture description here
If you want to view the report directly in Jenkins , You need to generate a fixed report name each time, the new one overwrites the old one, that is, do not add a timestamp in the html report, remove the timestamp in build.xml to
Insert picture description here
generate a test report
Insert picture description here
6. Start building the
Insert picture description here
ant build project, and execute it by default It is the script under the workspace in Jenkins. If there is no script under the workspace, the build will report an error.
6.1 View the Jenkins directory through Jenkins-System Management-System Settings-Home Directory Insert picture description here
6.2 Modify the workspace path
6.2.1. Find the Jenkins installation root directory and look for config. xml file
Insert picture description here6.2.2 In the config.xml file, look for the workspaceDir keyword and replace the address of your custom workspace root directory with the default address.
Insert picture description here
6.2.3 Restart to make the configuration file take effect
. Jenkins installed using msi needs to run the Jenkins command To re-read the configuration, click Read Settings in the system management to restart Jenkins
Insert picture description here
6.3, the build time reported that'ant.bat' is not an internal or external command, nor is it an executable program.
6.3.1 Enter the system management-global tool configuration
Insert picture description here
6.3.2 Enter the task configuration to reconfigure Invoke Ant
Insert picture description here
6.4 on the project homepage, click Build Now
Insert picture description here
6.5 to open the test report storage path to view the generated report
4. Send the report to the mailbox after each build
1. Enter the system management-system settings, configure Extended E-mail Notification-"Advanced"
Insert picture description here
Insert picture description here
test configuration by sending a test mail
Insert picture description here
2. Enter the project configuration-add the post-build operation
Insert picture description here
Insert picture description here
in the mail content Default Content Attach email template

<!DOCTYPE html>  
<html>  
<head>  
<meta charset="UTF-8">  
<title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>  
</head>  
  
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"  
    offset="0">  
    <h3>以下是Jenkins自动发送的邮件,请勿回复!</h3>
    <div>
    <table width="95%" cellpadding="0" cellspacing="0" 
        style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif"> 
        <tr>
            <th><br />
                <h2>构建信息</h2> 
            </th>
        </tr>
        <tr>  
            <td>  
                <ul>  
                    <li>项目名称 : ${PROJECT_NAME}</li><br />  
                    <li>构建日志 :<a href="${PROJECT_URL}${BUILD_NUMBER}/allure">${PROJECT_URL}${BUILD_NUMBER}/allure</a></li><br /> 
                    <li>触发原因: ${CAUSE}</li><br />                    
                    <li>项目  Url : <a href="${PROJECT_URL}">${PROJECT_URL}</a></li><br />  
                </ul>  
            </td> 
        </tr>  
    </table> 
    </div>
    <div>
    <table> 
        <tr>  
            <th><br />
            <h2>测试报告</h2>
            </th>  
        </tr>

        <tr>
            <td>
                <li>测试报告: <a href="${PROJECT_URL}/HTML_20Report">${PROJECT_URL}/HTML_20Report</a></li>               
            </td>
        </tr>

    </table> 
    </div>
     
  </body>  
</html>

Guess you like

Origin blog.csdn.net/test_number1/article/details/113860639