以下是配置背景
生成电子邮件内容的groovy脚本使用robot APIs来获取数据(例如:总测试,总失败,通过百分比)以及有关所有失败测试用例的信息。
对于所有失败的测试用例,RoboCaseResult列表,我包括:
目前正在运作。这是我的Groovy脚本实现的目的:
def actionslist = build.actions // List<hudson.model.Action>
def doRobotResultsExist = false
actionslist.each() { action ->
if( action.class.simpleName.equals("RobotBuildAction") ) { // hudson.plugins.robot.RobotBuildAction
doRobotResultsExist = true
displaycritical = (action.getOverallPassPercentage() != action.getCriticalPassPercentage())
%>
<h3>RobotFramework Results</h3>
<table>
<tr>
<td>Detailed Report:</td>
<td><a href="${rooturl}${build.url}robot/report/<%= action.getLogHtmlLink() %>" target="_new"><%= action.getLogHtmlLink() %></a></td>
</tr>
<!--
<tr>
<td>Pass Percentage:</td>
<td><%= action.overallPassPercentage %>%</td>
</tr>
-->
<tr>
<td>Overall Pass Ratio:</td>
<td><%= action.getTotalCount() - action.getFailCount() %>/<%= action.getTotalCount() %></td>
</tr>
<tr>
<td>Pass Percentage:</td>
<td><%= action.getOverallPassPercentage() %>%</td>
</tr>
<%
if (displaycritical) {
%>
<tr>
<td>Critical Pass Percentage:</td>
<td><%= action.getCriticalPassPercentage() %>%</td>
</tr>
<% } //if displaycrital %>
<tr>
<td>Total Failed:</td>
<td><%= action.getFailCount() %></td>
</tr>
</table>
<%
//action.result returns hudson.plugins.robot.model.RobotResult
//action.retult.getAllFailedCases() returns a list of hudson.plugins.robot.model.RobotCaseResult
def allFailedTests = action.result.getAllFailedCases() // hudson.plugins.robot.model.RobotCaseResult
if (!allFailedTests.isEmpty()) {
i = 0
%>
<table cellspacing='0' cellpadding='1' border='1'>
<tr class='bg1'>
<% if (displaycritical) { %><th>Tagged Critical</th><% } //if displaycrital %>
<th>Suite</th>
<th>Failed Test Case</th>
<th>Error message</th>
</tr>
<%
//allFailedTests.each() { testresult ->
// def testCaseResult = testresult
allFailedTests.each() { testCaseResult ->
i++
print "<tr " + ( (i % 2) == 0 ? "class='bg2'" : "") + " >"
if (displaycritical) {
print "<td>" + (testCaseResult.isCritical()? "<font color='red'><b>YES</b></font>": "no" )+ "</td>"
}
print "<td>" + testCaseResult.getParent().getRelativePackageName(testCaseResult.getParent()) + "</td>"
print "<td>" + testCaseResult.getDisplayName() + "</td>"
print "<td>" + testCaseResult.getErrorMsg() + "</td>"
print "</tr>"
} // for each failed test
%>
</table>
<%
} // if list of failed test cases is not empty
} // end action is RobotBuildAction
} // end of each actions
这产生了类似的东西
========================================================================= | Tagged Critical | Suite | Failed Test Case | Error Message | ========================================================================= | YES | Fruits | Get Apples | Could not get apples | +-----------------+--------+------------------+-------------------------+ | NO | Fruits | Eat Apple | Could not find an apple | =========================================================================
对于每个失败的测试用例, 我想要包含所有引发的警告 。但是我无法为此找到API,所以我打开了针对Jenkins插件的增强票。 Jenkins robotframework插件维护者可能无法在我需要解决方案的时间范围内回复我的请求。
如何通过groovy脚本包含机器人测试期间引发的所有警告?也就是说,我希望得到以下内容
================================================================================================ | Tagged Critical | Suite | Failed Test Case | Error Message | Warnings | ================================================================================================ | YES | Fruits | Get Apples | Could not get apples | No baskets available | +-----------------+--------+------------------+-------------------------+----------------------+ | NO | Fruits | Eat Apple | Could not find an apple | | ================================================================================================
答案 0 :(得分:3)
我不熟悉机器人插件API,因此我无法说明您想要的信息是否可供您使用。我做的知道的是,信息在由robot生成的output.xml文件中可用。这个文件很容易解析。这是使用一个日志关键字进行测试生成的文件的顶部:
<robot generated="20140527 19:46:02.095" generator="Robot 2.8.1 (Python 2.7.2 on darwin)">
<suite source="/tmp/example.robot" id="s1" name="Example">
<test id="s1-t1" name="Example of a warning">
<kw type="kw" name="BuiltIn.Log">
<doc>Logs the given message with the given level.</doc>
<arguments>
<arg>This is a warning</arg>
<arg>warn</arg>
</arguments>
...
另一种解决方案是创建一个自定义listener,用于侦听警告和错误消息,保存它们,然后在测试运行时动态创建电子邮件消息。每次收到end_test
消息时,您都可以打印出您已检测到的任何错误或警告。套件运行完毕后,您就可以发送电子邮件了。
以下是一个简单的例子,虽然它没有给出你想要的确切输出(我懒得计算每列的最大宽度):
class exampleListener():
ROBOT_LISTENER_API_VERSION = 2
def __init__(self, filename="messages.txt"):
self.outfile = open(filename, "w")
self.outfile.write("Errors and Warnings\n")
self.current_test = None
self.current_messages = []
self.current_suite = None
def start_suite(self, name, attrs):
self.current_suite = name
def start_test(self, name, attrs):
self.current_test = name
self.current_messages = []
def log_message(self, data):
self.current_messages.append((data["level"], data["message"]))
def end_test(self, name, attrs):
for (type, text) in self.current_messages:
if type == "ERROR":
self.outfile.write("| %s | %s | %s\n" % (self.current_suite, self.current_test, text))
elif type == "WARN":
self.outfile.write("| %s | %s | | %s\n" % (self.current_suite, self.current_test, text))
self.current_messages = []
self.current_test = None
def end_suite(self, name, attrs):
self.outfile.close()
您可以使用--listener参数来使用它,例如:
pybot --listener ExampleListener.py my_suite