groovy - 如何在每个测试用例中获取 email-ext groovy 脚本中的所有 robotsframework 警告?

标签 groovy jenkins jenkins-plugins robotframework email-ext

背景

这是配置背景

  • 我有用 Robot Framework 编写的测试
  • 我正在使用 Jenkins 并安装了 robotsframework 和 email-ext 插件
    • 我有一项计划定期运行的 Jenkins 作业
      • jenkins 作业运行机器人测试
      • 然后发送一封包含测试结果的电子邮件(使用 email-ext)
      • 我正在使用 Groovy 脚本来生成电子邮件内容。

生成电子邮件内容的 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 robotsframework 插件维护者可能不会在我需要解决方案的时间内回复我的请求。

如何通过 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 |                      |
================================================================================================

最佳答案

我不熟悉机器人插件API,所以我不能说你是否可以从那里获得你想要的信息。我所知道的是,该信息可在由 robots.txt 生成的 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

关于groovy - 如何在每个测试用例中获取 email-ext groovy 脚本中的所有 robotsframework 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23899790/

相关文章:

grails - 为什么此wslite SOAP客户端代码不起作用?

gradle - Gradle任务doLast如果任务失败

java - 在 WebSphere 6.1 上使用 ant/jenkins 生成 EAR 的类转换异常

python - 在 Jenkins 作业中构建之前调用 python 脚本

Jenkins 计划使用环境变量构建触发器?

curl - 使用 cURL 在 Jenkins UI 中创建文件夹

unit-testing - 用多部分请求测试Grails Controller

jenkins - groovy 执行间歇性地不返回任何输出或错误

Jenkins:来自管道的 MatrixCombinationsParameterValue

php - Jenkins pdepend插件错误