Want to filter out failure Message from my robot framework output files

1.6k Views Asked by At

I want to filter failure messages from output files generated after executing my testcases in Robot Framework. I have tried modules like from robot.api import ExecutionResult but it gives me only only count of Passed and Failed Testcases.

I have also tried other Robot framework Libtraries like import robot.errors to filter out all error messages but didn't get any luck. Below is my code block: `

#!/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' ]`

Any example to get this work done will be very helpful.

1

There are 1 best solutions below

1
On BEST ANSWER

I have tried a lot to get my expected output by using Robot Framework APIs but didn't get proper solution. Finally I got my solution by using import xml.etree.ElementTree as ET module. By using xml.etree.ElementTree module I am parsing my robot result.xml file and getting my work done. `

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`