想从我的机器人框架输出文件中过滤掉失败的消息

时间:2015-06-16 04:53:36

标签: python robotframework

我想从Robot Framework中执行测试用例后生成的输出文件中过滤失败消息。我尝试过像 robot.api import ExecutionResult 这样的模块,但它只给出了通过和失败的测试用例的数量。

我还尝试了其他机器人框架Libtraries,例如 import robot.errors 来过滤掉所有错误消息,但没有运气。下面是我的代码块: `

#!/usr/bin/python
from robot.api import ExecutionResult
import robot.errors
from robot.result.visitor import ResultVisitor
xmlpath = "<output.xml PATH>"
result = ExecutionResult(xmlpath)
result.configure(stat_config={'suite_stat_level': 2,
                              'tag_stat_combine': 'tagANDanother'})
stats = result.statistics
print stats.total.critical.failed
print stats.total.critical.passed
print stats.total.critical.passed + stats.total.critical.failed

class FailureCollector(ResultVisitor):
    def __init__(self):
        self.failures = []

    def visit_test(self, test):
        if not test.passed:
        self.failures += [test]

failure_collector = FailureCollector()
result.visit(failure_collector)
print failure_collector.failures
#the above print gives me all failed testcases as a list Eg: ['test1:My example Testcase1','test2:My example Testcase2' ]`

任何完成这项工作的例子都会非常有用。

1 个答案:

答案 0 :(得分:0)

我已经尝试过使用Robot Framework API获得预期的输出,但没有得到正确的解决方案。最后,我使用 import xml.etree.ElementTree作为ET 模块获得了我的解决方案。通过使用 xml.etree.ElementTree 模块,我正在解析我的机器人result.xml文件并完成我的工作。 `

import xml.etree.ElementTree as ET
import re

tree = ET.parse('<output.xml file Path>')
root = tree.getroot()
testplans = <Testplans as a list>

i = 0
err_dict = {}
for testplan in testplans:
  full_err_list = []
  err_list = []
  for suite_level_1 in root:
      try:
        if suite_level_1.tag == "suite":
          for suite_level_2 in suite_level_1:
            if suite_level_2.tag == "suite" and suite_level_2.attrib['name'] == testplan:
              for suite_level_3 in suite_level_2:
                if suite_level_3.tag == "suite":
                  for test in suite_level_3:
                    if test.tag == "test":
                      for kw_level_5 in test:
                        if kw_level_5.tag == "kw" and kw_level_5.attrib['name'] == '<specific keyword under which you expect your result(error or Success message >':
                          for msg in kw_level_5:
                            if msg.tag == 'msg':
                              err_str = msg.text
                              #print err_str
                              mat = re.match(r'\$\{FinalResult\}\s=\s(.*)',err_str)
                              if mat and mat.group(1) != 'Succeeded.':
                                i=i+1
                                #print mat.group(1), i
                                err = mat.group(1)
                                full_err_list.append(err)
                                if err not in err_list:
                                    err_list.append(err)
      except:
        print "Errors found"
        break
      err_dict[testplan] = err_list
  print "\n##########  "+testplan+"  ##########\n"
  print "Total no of failures", len(full_err_list)
  for err_name in err_list:
    print err_name, "===>>", full_err_list.count(err_name)
##The above will print the error name and its count in specific testPlan`