1. 程式人生 > >Jenkins上配置Robot Framework測試郵件通知模板

Jenkins上配置Robot Framework測試郵件通知模板

郵件效果

測試成功如下所示:
jenkins_robot_success

測試失敗如下所示:

jenkins_robot_failure

通過這個模板,我們能夠很直觀地看出測試的執行情況,以及相關的統計資訊。接下來我們詳細介紹如何配置郵件模板

配置郵件模板

第一步編寫郵件模板

我們的郵件模板是基於groovy指令碼編寫的。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<style type="text/css">
/*base css*/
a{color:#4a72af}
body{background-color
:#e4e4e4
} body,p{margin:0;padding:0} img{display:block} h1,h2,h3,h4,h5,h6{margin:0 0 .8em 0} h3{font-size:28px;color:#444!important;font-family:Arial,Helvetica,sans-serif} h4{font-size:22px;color:#4a72af!important;font-family:Arial,Helvetica,sans-serif} h5{font-size:18px;color:#444!important;font-family:Arial,Helvetica,sans-serif
} p{font-size:12px;color:#444!important;font-family:"Lucida Grande","Lucida Sans","Lucida Sans Unicode",sans-serif;line-height:1.5} table.robotstat { border: 1px solid black; border-collapse: collapse; empty-cells: show; margin: 0px 1px; table-layout: fixed; word-wrap: break-word; font-size
: 1em
; border-width:1px; }
tr.test_column_robot { background-color:#C6C6C6; } ol li img{display:inline;height:20px} /*div styles*/ .news{text-align:center;padding-top:15px;} .content{width:720px;margin:0 auto;background-color:white} .round_border{margin-bottom:5px;-webkit-border-radius:6px;-moz-border-radius:6px;border-radius:6px;margin-top:0;font-size:14px;padding:6px;border:1px solid #ccc} .status{background-color:<%= build.result.toString() == "SUCCESS" ? 'green' : 'red' %>;font-size:28px;font-weight:bold;color:white;width:720px;height:52px;margin-bottom:18px;text-align:center;vertical-align:middle;border-collapse:collapse;background-repeat:no-repeat} .status .info{color:white!important;text-shadow:0 -1px 0 rgba(0,0,0,0.3);font-size:32px;line-height:36px;padding:8px 0} .main img{width:38px;margin-right:16px;height:38px} .main table{font-size:14px;} .main table th{text-align:right;} .bottom-message{width:720px;cellpadding:5px;cellspacing:0px} .bottom-message .message{font-size:13px;color:#aaa;line-height:18px;text-align:center} .bottom-message .designed{font-size:13px;color:#aaa;line-height:18px;font-style: italic;text-align:right} img.cartoon {width: 36px; display:inline}
</style> <body> <div class="content round_border"> <div class="status"> <p class="info">構建狀態 <%= build.result.toString().toLowerCase() %></p> </div> <!-- status --> <div class="main round_border"> <table> <tbody> <tr> <th>專案名稱:</th> <td>${project.name}</td> </tr> <tr> <th>構建輪次:</th> <td><a href="${rooturl}${build.url}">${build.displayName}(點選檢視此輪構建資訊)</a></td> </tr> <tr> <th>構建時間:</th> <td>${it.timestampString}</td> </tr> <tr> <th>構建時長:</th> <td>${build.durationString}</td> </tr> <tr> <th>構建緣由:</th> <td><% build.causes.each() { cause -> %> ${cause.shortDescription} <% } %></td> </tr> <tr> <th>測試報告:</th> <td><a href="${rooturl}${build.url}robot">點選檢視測試報告詳情</a></td> </tr> <tr> <!-- test stat --> <th>測試統計:</th></br> <td> <table id="robotstat" class="robotstat"> <thead> <tr id="test_column_robot" class="test_column_robot"> <th>測試總用例數</th> <th>失敗用例數</th> <th>測試通過率</th> </tr> </thead> <tbody> <tr> <% def robotTestResultAction = it.getAction("hudson.plugins.robot.RobotBuildAction") %> <td>${robotTestResultAction.getTotalCount()}</td> <td>${robotTestResultAction.getFailCount()}</td> <td>${robotTestResultAction.getOverallPassPercentage()}%</td> </tr> </tbody> </table> </td> </tr> <tr> <th>變更記錄:</th> <td><a href="${rooturl}${build.url}changes">點選檢視變更記錄</a></td> </tr> <tr> <td colspan="2"> </td> </tr> </tbody> </table> </div> <!-- main --> <% def artifacts = build.artifacts if(artifacts != null && artifacts.size() > 0) { %> <div class="artifacts round_border"> <b>Build Artifacts:</b> <ul> <% artifacts.each() { f -> %> <li><a href="${rooturl}${build.url}artifact/${f}">${f}</a></li> <% } %> </ul> </div> <% } %> <!-- artifacts --> <% def changeSet = build.changeSet if(changeSet != null) { def hadChanges = false def count = 0 %> <div class="details round_border"> <b>變更詳細:</b> <ol> <% changeSet.each() { cs -> hadChanges = true def aUser = cs.author %> <li>${cs.msgAnnotated} (${aUser.displayName}) (<a href="${rooturl}${build.url}changes#detail${count}">detail</a>)</li> <% count ++ } %> </ol> </div> <% } %> <!-- details --> </div> <!-- content --> <table class="bottom-message" align="center"> <tr> <td class="message">You are receiving this email because you are relavent with this build<br> </td> </tr> <tr> <td colspan="2" class="designed">designed by @wangyang  </td> </tr> </table> <!-- bottom message --> </body>

這個郵件模板是基於groovy-html-larry.template模板改造的,我們將模板檔案放到$JENKINS_HOME/email-templates目錄下,如果沒有這個目錄,自己建立該目錄。

第二步配置郵件

這裡,我們需要在Jenkins安裝email-ext外掛,配置如下圖所示:

jenkins_robot_email_template

這樣的話,我們就完成了郵件模板的配置了。如果郵件傳送設定正確的話,我們應該可以收到測試郵件通知。

其他

我們還可以自己編寫HTML的郵件模板,而不用email-ext外掛,下面是我配置效果, 執行完成後,會收到這樣的郵件通知。

test_fail_email_report

test_pass_report