首先,保證本地安裝并部署了jenkins,jmeter,xslproc

我搭建的自動(dòng)化測(cè)試框架是jmeter+jenkins+xslproc

---注意:原理是,jmeter自生成的報(bào)告jtl文件,通過(guò)xslproc工具,再結(jié)合jmeter自帶的模板修改,自帶模板目錄如下,最終將測(cè)試結(jié)果報(bào)告轉(zhuǎn)化為html格式

1.jenkins新建一個(gè)任務(wù),選擇“構(gòu)建一個(gè)自由風(fēng)格的軟件項(xiàng)目”,點(diǎn)擊OK

 

 2.選擇構(gòu)建觸發(fā)器,根據(jù)時(shí)間點(diǎn),每天到點(diǎn)進(jìn)行自動(dòng)觸發(fā)構(gòu)建

3.輸入構(gòu)建命令

(1)D:/soft/apache-jmeter-3.2/bin/jmeter -n -t D:/soft/apache-jmeter-3.2/jmx/接口測(cè)試.jmx 

這一句是通過(guò)cmd命令調(diào)用jmeter程序,執(zhí)行jmeter的jmx自動(dòng)化腳本;

(2)D:/soft/xsltproc/bin/xsltproc D:/soft/apache-jmeter-3.2/xsl/jmeter-results-detail-report_new.xsl %WORKSPACE%/test.jtl>%WORKSPACE%/test.html

這一句是調(diào)用xslproc程序,通過(guò)jmeter-results-detail-report_new.xsl模板,將jtl文件轉(zhuǎn)換為html報(bào)告文件;

4.

郵件的格式代碼如下:

大家好,本次接口測(cè)試構(gòu)建結(jié)果如下:
<hr/>

DOCTYPE html >
<html>
<head>
<meta name="referrer" content="never">
<title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次構(gòu)建日志title>
head>

<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"    offset="0">
    <table width="95%" cellpadding="0" cellspacing="0"
        style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
        <tr>
            <td>(本郵件是程序自動(dòng)下發(fā)的,請(qǐng)勿回復(fù)!)td>
        tr>
        <tr>
            <td><h2>
                    <font color="#0000FF">構(gòu)建結(jié)果 - ${BUILD_STATUS}font>
                h2>td>
        tr>
        <tr>
            <td><br />
            <b><font color="#0B610B">構(gòu)建信息font>b>
            <hr size="2" width="100%" align="center" />td>
        tr>
        <tr>
            <td>
                <ul>
                    <li>項(xiàng)目名稱(chēng)  ${PROJECT_NAME}li>
                    <li>構(gòu)建編號(hào)  第${BUILD_NUMBER}次構(gòu)建li>
                    <li>觸發(fā)原因: ${CAUSE}li>
                    <li>構(gòu)建日志: <a href="${BUILD_URL}console">${BUILD_URL}consolea>li>
                    <li>構(gòu)建  Url  <a href="${BUILD_URL}">${BUILD_URL}a>li>
                    <li>工作目錄  <a href="${PROJECT_URL}ws">${PROJECT_URL}wsa>li>
                    <li>項(xiàng)目  Url  <a href="${PROJECT_URL}">${PROJECT_URL}a>li>
                ul>
            td>
        tr>
        <tr>
            <td><b><font color="#0B610B">Changes Since Last
                        Successful Build:font>b>
            <hr size="2" width="100%" align="center" />td>
        tr>
        <tr>
            <td>
                <ul>
                    <li>歷史變更記錄 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changesa>li>
                ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat="<pre>[%a]<br />%mpre>",pathFormat="    %p"}
            td>
        tr>
        <tr>
            <td><b>Failed Test Resultsb>
            <hr size="2" width="100%" align="center" />td>
        tr>
        <tr>
            <td><pre
                    style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTSpre>
                <br />td>
        tr>
    table>

body>
html>
<b>${FILE,path="D:/jenkins/workspace/test/test.html"}b>

以上是jenkins的所有設(shè)置

5.jenkins的版本太低的話(huà),可能存在需要手動(dòng)下載jenkins插件

 

 

最后,附上模板代碼給大家~

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">





<xsl:output method="html" indent="yes" encoding="GB18030"  doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />


<xsl:param    name="showData" select="'y'"/>
<xsl:param    name="titleReport" select="'接口測(cè)試'"/>


<xsl:template match="testResults">
    <html >
    
        <head>
            <title><xsl:value-of select="$titleReport" />title>
            <style type="text/css">
                
                body {
                    font:normal 95% verdana,arial,helvetica;
                    color:#000000;
                }
                table tr td, table tr th {
                    font-size: 95%;
                }
                table.details tr th{
                    word-wrap:break-word;
                    color: #ffffff;
                    font-weight: bold;
                    text-align:center;
                    background:#2674a6;
                    
                }
                table.details tr td{
                    width: 400px;
                    word-wrap:break-word;
                    background:#eeeee0;
                    
                }
                table{
                    table-layout:fixed ; 
                }
                h1 {
                    margin: 0px 0px 5px; font: 165% verdana,arial,helvetica
                }
                h2 {
                    margin-top: 1em; margin-bottom: 0.5em; font: bold 125% verdana,arial,helvetica
                }
                h3 {
                    margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica
                }
                .Failure {
                    font-weight:bold; color:red;
                }
                
    
                img
                {
                  border-width: 0px;
                }
                
                .expand_link
                {
                   position:absolute;
                   right: 0px;
                   width: 27px;
                   top: 1px;
                   height: 27px;
                }
                
                .page_details
                {
                   display: none;
                }
                                
                /* .page_details_expanded
                {
                    display: block;
                    displayhide this definition from  IE5/6: table-row;
                } */


            style>
            <script language="JavaScript">
                           function expand(details_id)
               {
                  
                  document.getElementById(details_id).className = "page_details_expanded";
               }
               
               function collapse(details_id)
               {
                  
                  document.getElementById(details_id).className = "page_details";
               }
               
               function change(details_id)
               {
                  if(document.getElementById(details_id+"_image").src.match("expand"))
                  {
                     document.getElementById(details_id+"_image").src = "collapse.png";
                     expand(details_id);
                  }
                  else
                  {
                     document.getElementById(details_id+"_image").src = "expand.png";
                     collapse(details_id);
                  } 
                           }
            ]]>script>
        head>
        <body >
        
            <xsl:call-template name="pageHeader" />
                <xsl:call-template name="myresult" />    
            <xsl:call-template name="summary" />
            <hr size="1" width="75%" align="center" />
            
        <xsl:call-template name="pagelist" />
            <hr size="1" width="75%" align="center" />
            
            <xsl:call-template name="detail" />

        body>
    
    html>
xsl:template>

<xsl:template name="pageHeader">
    <h1><xsl:value-of select="$titleReport" />h1>
    <table width="100%">
        <tr>
            <td align="left">Date report:td>
            <td align="right">from 一卡通td>
        tr>
    table>
    <hr size="1" />
xsl:template>

<xsl:template name="myresult">

<xsl:variable name="allFailCount" select="count(/testResults/*[attribute::s='false'])" />

<xsl:if test="$allFailCount = '0'">
 <h2>測(cè)試結(jié)果:成功h2>
xsl:if>
<xsl:if test="$allFailCount > '0'">
  <h2  bgcolor="#ff00ff">測(cè)試結(jié)果:失敗h2>
  <h2  bgcolor="#ff00ff">失敗個(gè)數(shù):<xsl:value-of select="count(/testResults/*[attribute::s='false'])"/>h2>

xsl:if>                    

  

xsl:template>


<xsl:template name="summary">
    <h2>報(bào)告概況h2>
    <table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
        <tr valign="top">
            <th>用例總數(shù)th>
            <th>失敗數(shù)th>
            <th>成功率th>
            <th>平均響應(yīng)時(shí)間th>
        tr>
        <tr valign="top">
            <xsl:variable name="allCount" select="count(/testResults/*)" />
            <xsl:variable name="allFailureCount" select="count(/testResults/*[attribute::s='false'])" />
            <xsl:variable name="allSuccessCount" select="count(/testResults/*[attribute::s='true'])" />
            <xsl:variable name="allSuccessPercent" select="$allSuccessCount div $allCount" />
            <xsl:variable name="allTotalTime" select="sum(/testResults/*/@t)" />
            <xsl:variable name="allAverageTime" select="$allTotalTime div $allCount" />
            <xsl:variable name="allMinTime">
                <xsl:call-template name="min">
                    <xsl:with-param name="nodes" select="/testResults/*/@t" />
                xsl:call-template>
            xsl:variable>
            <xsl:variable name="allMaxTime">
                <xsl:call-template name="max">
                    <xsl:with-param name="nodes" select="/testResults/*/@t" />
                xsl:call-template>
            xsl:variable>
            <xsl:attribute name="class">
                <xsl:choose>
                    <xsl:when test="$allFailureCount > 0">Failurexsl:when>
                xsl:choose>
            xsl:attribute>
            <td align="center">
                <xsl:value-of select="$allCount" />
            td>
            <td align="center">
                <xsl:value-of select="$allFailureCount" />
            td>
            <td align="center">
                <xsl:call-template name="display-percent">
                    <xsl:with-param name="value" select="$allSuccessPercent" />
                xsl:call-template>
            td>
            <td align="center">
                <xsl:call-template name="display-time">
                    <xsl:with-param name="value" select="$allAverageTime" />
                xsl:call-template>
            td>
    
        tr>
    table>
xsl:template>

<xsl:template name="pagelist">
    <h2>詳細(xì)情況h2>
    <table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
        <tr valign="top">
            <th width="50%">功能模塊th>
            <th>檢查次數(shù)th>
            <th>失敗數(shù)th>
            <th>成功率th>
            <th>平均響應(yīng)時(shí)間th>

        tr>
        <xsl:for-each select="/testResults/*[not(@tn = preceding::*/@tn)]">
            <xsl:variable name="label" select="@tn" />
            <xsl:variable name="count" select="count(../*[@tn = current()/@tn])" />
            <xsl:variable name="failureCount" select="count(../*[@tn = current()/@tn][attribute::s='false'])" />
            <xsl:variable name="successCount" select="count(../*[@tn = current()/@tn][attribute::s='true'])" />
            <xsl:variable name="successPercent" select="$successCount div $count" />
            <xsl:variable name="totalTime" select="sum(../*[@tn = current()/@tn]/@t)" />
            <xsl:variable name="averageTime" select="$totalTime div $count" />
            <xsl:variable name="minTime">
                <xsl:call-template name="min">
                    <xsl:with-param name="nodes" select="../*[@tn = current()/@tn]/@t" />
                xsl:call-template>
            xsl:variable>
            <xsl:variable name="maxTime">
                <xsl:call-template name="max">
                    <xsl:with-param name="nodes" select="../*[@tn = current()/@tn]/@t" />
                xsl:call-template>
            xsl:variable>
            
            
            <tr valign="top">
                <xsl:attribute name="class">
                    <xsl:choose>
                        <xsl:when test="$failureCount > 0">Failurexsl:when>
                    xsl:choose>
                xsl:attribute>
                <td>
                <xsl:if test="$failureCount > 0">
                  <a><xsl:attribute name="href">#<xsl:value-of select="$label" />xsl:attribute>
                  <xsl:value-of select="$label" />
                  a>
                xsl:if>
                <xsl:if test="0 >= $failureCount">
                  <xsl:value-of select="$label" />
                xsl:if>
                td>
                <td align="center">
                    <xsl:value-of select="$count" />
                td>
                <td align="center">
                    <xsl:value-of select="$failureCount" />
                td>
                <td align="center">
                    <xsl:call-template name="display-percent">
                        <xsl:with-param name="value" select="$successPercent" />
                    xsl:call-template>
                td>
                <td align="center">
                    <xsl:call-template name="display-time">
                        <xsl:with-param name="value" select="$averageTime" />
                    xsl:call-template>
                td>
            
            tr>
            

        xsl:for-each>
    table>
xsl:template>

<xsl:template name="detail">
    <xsl:variable name="allFailureCount" select="count(/testResults/*[attribute::s='false'])" />

    <xsl:if test="$allFailureCount > 0">
        <h2>Failure Detailh2>

        <xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]">       
            <xsl:variable name="failureCount" select="count(../*[@lb = current()/@lb][attribute::s='false'])" />          
            <xsl:if test="$failureCount > 0">
                <xsl:for-each select="/testResults/*[@lb = current()/@lb][attribute::s='false']">            
                <h3><xsl:value-of select="@tn" />---<xsl:value-of select="@lb" /><a><xsl:attribute name="name"><xsl:value-of select="@tn" />xsl:attribute>a>h3>
                

                <table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
                <tr valign="top">
                    <th>http請(qǐng)求th>
                    <th>失敗詳細(xì)信息th>
                    <xsl:if test="$showData = 'y'">
                       <th>響應(yīng)數(shù)據(jù)th>
                    xsl:if>
                tr>
            
                
                    <tr>
                        <td><xsl:value-of select="java.net.URL" /> td>
                        <td><xsl:value-of select="assertionResult/failureMessage" />td>
                        <xsl:if test="$showData = 'y'">
                            <td><xsl:value-of select="responseData" />td>
                        xsl:if>
                    tr>
                
                
                table>
                xsl:for-each>
            xsl:if>
           
        xsl:for-each>
    xsl:if>
xsl:template>

<xsl:template name="min">
    <xsl:param name="nodes" select="/.." />
    <xsl:choose>
        <xsl:when test="not($nodes)">NaNxsl:when>
        <xsl:otherwise>
            <xsl:for-each select="$nodes">
                <xsl:sort data-type="number" />
                <xsl:if test="position() = 1">
                    <xsl:value-of select="number(.)" />
                xsl:if>
            xsl:for-each>
        xsl:otherwise>
    xsl:choose>
xsl:template>

<xsl:template name="max">
    <xsl:param name="nodes" select="/.." />
    <xsl:choose>
        <xsl:when test="not($nodes)">NaNxsl:when>
        <xsl:otherwise>
            <xsl:for-each select="$nodes">
                <xsl:sort data-type="number" order="descending" />
                <xsl:if test="position() = 1">
                    <xsl:value-of select="number(.)" />
                xsl:if>
            xsl:for-each>
        xsl:otherwise>
    xsl:choose>
xsl:template>

<xsl:template name="display-percent">
    <xsl:param name="value" />
    <xsl:value-of select="format-number($value,'0.00%')" />
xsl:template>

<xsl:template name="display-time">
    <xsl:param name="value" />
    <xsl:value-of select="format-number($value,'0 ms')" />
xsl:template>
    
xsl:stylesheet>

模板的截圖如下: