要求:
1.robotframework结合jenkins,实现集成构建
2.构建后能解析执行结果,并邮件发送给特定人的邮箱
3.邮件内容可以自定义,做到美观(自带的邮件内容不美观),下图是自定义模版
我的前置条件:
1.Jenkins部署在Windows环境下(如果Jenkins部署在Linux环境下,robotframework最好也搭建在Linux下)
2.robotframework也在Windows环境下,脚本没放到svn上,本次内容不涉及svn(jenkins结合本地的脚本跑的)
3.已经安装了Jenkins(下载地址https://jenkins.io/download/)
步骤:
一。登录Jenkins,系统设置
1.下载插件
系统管理-插件管理,选择“可选插件”tab页,在过滤框中依次搜索并下载:Robot Framework plugin,Email Extension Plugin,Zentimestamp plugin
2.系统设置邮件配置:系统管理-系统设置(只改图中的地方,其他不用管)
二。新建job
1.新建任务,选择“构建一个自由风格的软件项目”,点击确定
2.1因为我脚本没放在svn上,所以这边选择无
2.2构建选择执行Windows批处理命令,其中的路径是你脚本的存放路径,其他不变
3.3构建后操作,选择publish robot framework test results,路径是执行脚本后存放log地址,例如log.html,report.html等文件的目录,下面数字不用管了
3.4 构建后操作,再选择个editable email notification。配置信息如下
下图中照选。如果没有此页面点击advaned。其中reciplent list就是系统设置中邮箱收件人
三。自定义邮件内容
找到Jenkins按照目录,我的在d:/jenkins
目录下有个文件夹叫email-templates,没有自己新建个
在该文件夹下新建个文档,命名为:robot_results.groovy。里面内容:
<%
import java.text.DateFormat
import java.text.SimpleDateFormat
%>
<!-- Robot Framework Results -->
<%
def robotResults = false
def actions = build.actions // List<hudson.model.Action>
actions.each() { action ->
if( action.class.simpleName.equals("RobotBuildAction") ) { // hudson.plugins.robot.RobotBuildAction
robotResults = true %>
<div style="width:100%;float:left">
<table cellspacing="0" cellpadding="4" border="1" align="left">
<thead>
<tr bgcolor="#F3F3F3">
<td style="text-align:center" colspan="4"><b>自动化测试汇总报告</b></td>
</tr>
<tr>
<td bgcolor="#F3F3F3" style="width:80px"><b>详细报告:</b></td>
<td colspan="4"><a href="${rooturl}${build.url}robot/report/report.html">点击查看报告详情</a></td>
</tr>
<tr bgcolor="#F3F3F3">
<td><b>用例总数</b></td>
<td><b>通过</b></td>
<td style="width:60px"><b>不通过</b></td>
<td style="width:100px"><b>通过率</b></td>
</tr>
<tr>
<td><%= action.result.overallTotal %></td>
<td><b><span style="color:#66CC00"><%= action.result.overallPassed %></span></b></td>
<td><b><span style="color:#FF3333"><%= action.result.overallFailed %></span></b></td>
<td><%= action.overallPassPercentage %>%</td>
</tr>
<tr bgcolor="#F3F3F3">
<td colspan="2"><b>Test Name</b></td>
<td><b>Status</b></td>
<td><b>Elapsed Time</b></td>
</tr>
</thead>
<tbody>
<% def suites = action.result.allSuites
suites.each() { suite ->
def currSuite = suite
def suiteName = currSuite.displayName
// ignore top 2 elements in the structure as they are placeholders
while (currSuite.parent != null && currSuite.parent.parent != null) {
currSuite = currSuite.parent
suiteName = currSuite.displayName + "." + suiteName
} %>
<tr>
<td colspan="4"><b><%= suiteName %></b></td>
</tr>
<% DateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss")
def execDateTcPairs = []
suite.caseResults.each() { tc ->
Date execDate = format.parse(tc.starttime)
execDateTcPairs << [execDate, tc]
}
// primary sort execDate, secondary displayName
execDateTcPairs = execDateTcPairs.sort{ a,b -> a[1].displayName <=> b[1].displayName }
execDateTcPairs = execDateTcPairs.sort{ a,b -> a[0] <=> b[0] }
execDateTcPairs.each() {
def execDate = it[0]
def tc = it[1] %>
<tr>
<td colspan="2"><%= tc.displayName %></td>
<td><b><span style="color:<%= tc.isPassed() ? "#66CC00" : "#FF3333" %>"><%= tc.isPassed() ? "PASS" : "FAIL" %></span></b></td>
<td><%= tc.getDuration().intdiv(60000)+"分"+(tc.getDuration()-tc.getDuration().intdiv(60000)*60000).intdiv(1000)+"秒" %></td>
</tr>
<% if(tc.errorMsg != null) {%>
<tr>
<td ><b><span style="font-size:10px;color:#FF3333">错误描述:</span></b></td>
<td colspan="3"><span style="font-size:10px"><%= tc.errorMsg%></span></td>
</tr>
<%
}%>
<% } // tests
} // suites %>
</tbody>
</table>
<p style="color:#AE0000;clear:both">*这个是通过Jenkins自动构建得出的报告,仅供参考。</p>
</div>
<%
} // robot results
}
if (!robotResults) { %>
<p>No Robot Framework test results found.</p>
<%
} %>
大功告成,去Jenkins里构建下,就能收到邮件了。
参考博客: https://blog.csdn.net/ojiawang/article/details/51872481