玩命加载中 . . .

Jenkins Email发送HTML测试报告模板


概述

最近打算使用nose framework写产品自动化用例,需要与jenkins集成做CI,将测试结果以Email形式广播给大家,于是尝试搞一个html模板,将HTML测试结果以附件方式发送出去。

实测效果

Jenkins设置’Inject enviromment variables’

这里需要在Jenkins中设置环境变量,写入到这个变量文件中,有一个shell脚本,从这个变量文件中读取需要的信息,传递给HTML模板,从而能够构成一个完整的HTML数据,发送出去.
Jenkins上设置变量信息如下图所示:

读取变量文件的脚本

#!/bin/bash

scriptrootpath=/work/automation-test/nose_7.0/nose_framework
if [ ! -d ${scriptrootpath} ]; then
    mkdir ${scriptrootpath}
    chown -R jenkins.jenkins ${scriptrootpath}
fi


# Get automation start and end time for jenkins report'
time_file="$scriptrootpath/report/time.txt"
START_TIME=`cat ${time_file} | head -n 1`
echo START_TIME=${START_TIME} > $scriptrootpath/build.properties
END_TIME=`tac ${time_file} | head -n 1`
echo END_TIME=${END_TIME} >> $scriptrootpath/build.properties

# Record version
version_file="$scriptrootpath/report/version.txt"
PRODUCT_VERSION=`cat ${version_file}`
echo PRODUCT_VERSION=${PRODUCT_VERSION} >> $scriptrootpath/build.properties

#parse result.txt
report_file="$scriptrootpath/report/all_test_cases.html"
if [ ! -f ${report_file} ]; then
    echo "[ERROR]  Not exist ${report_file}, exit!"
    exit
fi


result_file="$scriptrootpath/report/result.txt"
`cat ${report_file} | grep -A5 "<td><strong>Total</strong></td>" | grep -v strong | awk -F "td" '{{print $2}}' | sed 's/>//g' | sed 's/<\///g' > ${result_file}` 


test_counts=`awk 'NR==5, NR==5 {print $1}' ${result_file}`
test_pass=`awk 'NR==4, NR==4 {print $1}' ${result_file}`
test_fail=`awk 'NR==1, NR==1 {print $1}' ${result_file}`
test_skip=`awk 'NR==3, NR==3 {print $1}' ${result_file}`
test_error=`awk 'NR==2, NR==2 {print $1}' ${result_file}`

echo TEST_COUNTS=${test_counts} >> $scriptrootpath/build.properties
echo TEST_PASS=${test_pass} >> $scriptrootpath/build.properties
echo TEST_FAIL=${test_fail} >> $scriptrootpath/build.properties
echo TEST_SKIP=${test_skip} >> $scriptrootpath/build.properties
echo TEST_ERROR=${test_error} >> $scriptrootpath/build.properties

html模板代码

<html>
    <head>
        <title></title>
    </head>
    <body>
        <h2 style="color: #5e9ca0; text-align: center;">
            Note:(This email generated by system automatically, please do not reply!)
        </h2>
        <table class="editorDemoTable" style="height: 549px; width: 811px;">
            <thead>
                <tr>
                    <td style="background-color: #3498db; text-align: center; width: 801px;" colspan="2" nowrap="nowrap">
                        <h2>
                            <span style="color: #000000;">NOSE Automation Test Result</span>
                        </h2>
                    </td>
                </tr>
                <tr>
                    <td style="background-color: #3498db; width: 801px; text-align: left;" colspan="2" nowrap="nowrap">
                        <span style="color: #000000;">Build Information</span>
                    </td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td style="background-color: #beedc7; width: 150px;" nowrap="nowrap">
                        <span style="color: #000000;">Test Result</span>
                    </td>
                    <td style="background-color: #beedc7; width: 643px;" nowrap="nowrap">
                        <span style="color: #000000;">&nbsp;</span>
                        <ul>
                            <li>
                                <span style="color: #000000;">Total Cases:&nbsp; &nbsp; ${TEST_COUNTS}</span>
                            </li>
                            <li>
                                <span style="color: #000000; background-color: #008000;">Pass&nbsp; Cases:&nbsp; &nbsp; ${TEST_PASS}</span>
                            </li>
                            <li>
                                <span style="color: #000000; background-color: #ffff00;">Fail&nbsp; &nbsp;Cases:&nbsp; &nbsp; ${TEST_FAIL}</span>
                            </li>
                            <li>
                                <span style="color: #000000; background-color: #3366ff;">Skip&nbsp; Cases:&nbsp; &nbsp; ${TEST_SKIP}</span>
                            </li>
                            <li>
                                <span style="color: #000000; background-color: #ff0000;">Error Cases:&nbsp; &nbsp; ${TEST_ERROR}</span>
                            </li>
                        </ul>
                    </td>
                </tr>
                <tr>
                    <td style="background-color: #beedc7; width: 150px;" nowrap="nowrap">
                        <span style="color: #000000;">Product Version</span>
                    </td>
                    <td style="background-color: #beedc7; width: 643px;" nowrap="nowrap">
                        <span style="color: #000000;">$PRODUCT_VERSION</span>
                    </td>
                </tr>
                <tr>
                    <td style="background-color: #beedc7; width: 150px;" nowrap="nowrap">
                        Time Consuming
                    </td>
                    <td style="background-color: #beedc7; width: 643px;" nowrap="nowrap">
                        <span style="color: #000000;">From ($START_TIME) To ($END_TIME)</span>
                    </td>
                </tr>
                <tr>
                    <td style="background-color: #beedc7; width: 150px;" nowrap="nowrap">
                        <span style="color: #000000;">Project Name</span>
                    </td>
                    <td style="background-color: #beedc7; width: 643px;" nowrap="nowrap">
                        <span style="color: #000000;">$PROJECT_NAME</span>
                    </td>
                </tr>
                <tr>
                    <td style="background-color: #beedc7; width: 150px;" nowrap="nowrap">
                        <span style="color: #000000;">Build Number</span>
                    </td>
                    <td style="background-color: #beedc7; width: 643px;" nowrap="nowrap">
                        <span style="color: #000000;">$BUILD_NUMBER</span>
                    </td>
                </tr>
                <tr>
                    <td style="background-color: #beedc7; width: 150px;" nowrap="nowrap">
                        <span style="color: #000000;">Build Status</span>
                    </td>
                    <td style="background-color: #beedc7; width: 643px;" nowrap="nowrap">
                        <span style="color: #000000;">$BUILD_STATUS</span>
                    </td>
                </tr>
                <tr>
                    <td style="background-color: #beedc7; width: 150px;" nowrap="nowrap">
                        <span style="color: #000000;">Trigger Reason</span>
                    </td>
                    <td style="background-color: #beedc7; width: 643px;" nowrap="nowrap">
                        <span style="color: #000000;">${CAUSE}</span>
                    </td>
                </tr>
                <tr>
                    <td style="background-color: #3498db; width: 801px; text-align: left;" colspan="2" nowrap="nowrap">
                        <span style="color: #000000;">Test Information</span>
                    </td>
                </tr>
                <tr>
                    <td style="background-color: #beedc7; border-color: gray; width: 150px;" nowrap="nowrap">
                        <span style="color: #000000;">Build&nbsp;Result</span>
                    </td>
                    <td style="background-color: #beedc7; border-color: gray; width: 643px;" nowrap="nowrap">
                        <span style="color: #000000;"><a style="color: #000000;" title="Build Result" href="${BUILD_URL}console">${BUILD_URL}</a></span>
                    </td>
                </tr>
                <tr>
                    <td style="background-color: #beedc7; border-color: gray; width: 150px;" nowrap="nowrap">
                        <span style="color: #000000;">Build Log</span>
                    </td>
                    <td style="background-color: #beedc7; border-color: gray; width: 643px;" nowrap="nowrap">
                        <span style="color: #000000;"><a style="color: #000000;" title="Build Log" href="${BUILD_URL}console">${BUILD_URL}console</a></span>
                    </td>
                </tr>
                <tr>
                    <td style="background-color: #beedc7; border-color: gray; width: 150px;" nowrap="nowrap">
                        <span style="color: #000000;">Log&amp;Report</span>
                    </td>
                    <td style="background-color: #beedc7; border-color: gray; width: 643px;" nowrap="nowrap">
                        <br>
                        <ul>
                            <li>
                                <span style="color: #000000;">Test Report:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a style="color: #000000;" title="Test Report" href="${BUILD_URL}testReport">${BUILD_URL}testReport</a></span>
                            </li>
                            <li>
                                <span style="color: #000000;">Pylint Violations Report:&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <a style="color: #000000;" title="Pylint Violations Report" href="${BUILD_URL}violations">${BUILD_URL}violations</a></span>
                            </li>
                            <li>
                                <span style="color: #000000;">Cobertura Coverage Report:&nbsp;&nbsp; <a style="color: #000000;" title="Cobertura Coverage Report" href="${BUILD_URL}cobertura">${BUILD_URL}cobertura</a></span>
                            </li>
                        </ul>
                    </td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

Email中html效果


文章作者: Gavin Wang
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Gavin Wang !
  目录