blob: f3174f4639b27807bea5d5957cedef40127dd67b [file] [log] [blame]
#!/usr/bin/python
r"""
#############################################################
# @brief Uses robot framework API to extract test result
# data from output.xml generated by robot tests.
# For more information on the Robot Framework API
# http://robot-framework.readthedocs.io/en/3.0
# /autodoc/robot.result.html
#############################################################
"""
import sys
import getopt
import csv
from datetime import datetime
import robot.errors
from robot.api import ExecutionResult
from robot.result.visitor import ResultVisitor
def parse_output_xml(i_source, i_dest):
result = ExecutionResult(i_source)
result.configure(stat_config={'suite_stat_level': 2,
'tag_stat_combine': 'tagANDanother'})
stats = result.statistics
print "--------------------------------------"
print "Total Test Count:\t",\
stats.total.critical.passed + stats.total.critical.failed
print "Total Test Failed:\t", stats.total.critical.failed
print "Total Test Passed:\t", stats.total.critical.passed
print "Test Start Time:\t", result.suite.starttime
print "Test End Time:\t\t", result.suite.endtime
print "--------------------------------------"
# Use ResultVisitor object and save off the test data info
class TestResult(ResultVisitor):
def __init__(self):
self.testData = []
def visit_test(self, test):
self.testData += [test]
collectDataObj = TestResult()
result.visit(collectDataObj)
# Write the result statistics attributes to CSV file
l_csvlist = []
l_subsys = 'OPENBMC'
l_test_type = 'FTC'
l_pse_rel = 'OBMC910'
# Default header
l_header = ['test_start', 'test_end', 'subsys', 'test_type',
'test_result', 'test_name', 'pse_rel']
l_csvlist.append(l_header)
# Generate CSV file onto the path with current time stamp
l_base_dir = i_dest
l_timestamp = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S")
l_csvfile = l_base_dir + l_timestamp + ".csv"
print "Writing data into csv file:", l_csvfile
for testcase in collectDataObj.testData:
# Test category : Test case type
l_test_name = str(testcase.parent) + ":" + str(testcase)
# Test Result pass=0 fail=1
if testcase.status == 'PASS':
l_test_result = 0
else:
l_test_result = 1
# Data Sequence: test_start,test_end,subsys,test_type,
# test_result,test_name,pse_rel
l_data = [testcase.starttime, testcase.endtime, l_subsys,
l_test_type, l_test_result, l_test_name, l_pse_rel]
l_csvlist.append(l_data)
# Open the file and write to the CSV file
l_file = open(l_csvfile, "w")
l_writer = csv.writer(l_file, lineterminator='\n')
l_writer.writerows(l_csvlist)
l_file.close()
def usage():
name = 'gen_csv_results.py'
print 'Usage: '
print name, '-s <source path> -d <destination path>'
print '\t-s | --source= : output.xml robot test result file path'
print '\t-d | --dest= : Where the *.csv file will be generated'
sys.exit()
def main(argv):
source = ''
dest = ''
try:
opts, args = getopt.getopt(argv, "h:s:d:", ["source=", "dest="])
except getopt.GetoptError:
usage()
for opt, arg in opts:
if opt == '-h':
usage()
elif opt in ("-s", "--source"):
source = arg
elif opt in ("-d", "--dest"):
dest = arg
if source == '':
usage()
print 'ERROR: Provide input file path to robot generated output.xml '
if dest == '':
usage()
print 'ERROR: Destination directory where *.csv file will be generated'
parse_output_xml(source, dest)
if __name__ == "__main__":
main(sys.argv[1:])