George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | r""" |
| 3 | ############################################################# |
| 4 | # @brief Uses robot framework API to extract test result |
| 5 | # data from output.xml generated by robot tests. |
| 6 | # For more information on the Robot Framework API |
| 7 | # http://robot-framework.readthedocs.io/en/3.0 |
| 8 | # /autodoc/robot.result.html |
| 9 | ############################################################# |
| 10 | """ |
| 11 | import sys |
| 12 | import getopt |
| 13 | import csv |
| 14 | from datetime import datetime |
| 15 | import robot.errors |
| 16 | from robot.api import ExecutionResult |
| 17 | from robot.result.visitor import ResultVisitor |
| 18 | |
| 19 | |
| 20 | def parse_output_xml(i_source, i_dest): |
| 21 | result = ExecutionResult(i_source) |
| 22 | result.configure(stat_config={'suite_stat_level': 2, |
| 23 | 'tag_stat_combine': 'tagANDanother'}) |
| 24 | |
| 25 | stats = result.statistics |
| 26 | print "--------------------------------------" |
| 27 | print "Total Test Count:\t",\ |
| 28 | stats.total.critical.passed + stats.total.critical.failed |
| 29 | print "Total Test Failed:\t", stats.total.critical.failed |
| 30 | print "Total Test Passed:\t", stats.total.critical.passed |
| 31 | print "Test Start Time:\t", result.suite.starttime |
| 32 | print "Test End Time:\t\t", result.suite.endtime |
| 33 | print "--------------------------------------" |
| 34 | |
| 35 | # Use ResultVisitor object and save off the test data info |
| 36 | class TestResult(ResultVisitor): |
| 37 | def __init__(self): |
| 38 | self.testData = [] |
| 39 | |
| 40 | def visit_test(self, test): |
| 41 | self.testData += [test] |
| 42 | |
| 43 | collectDataObj = TestResult() |
| 44 | result.visit(collectDataObj) |
| 45 | |
| 46 | # Write the result statistics attributes to CSV file |
| 47 | l_csvlist = [] |
| 48 | l_subsys = 'OPENBMC' |
| 49 | l_test_type = 'FTC' |
| 50 | l_pse_rel = 'OBMC910' |
| 51 | |
| 52 | # Default header |
| 53 | l_header = ['test_start', 'test_end', 'subsys', 'test_type', |
| 54 | 'test_result', 'test_name', 'pse_rel'] |
| 55 | |
| 56 | l_csvlist.append(l_header) |
| 57 | |
| 58 | # Generate CSV file onto the path with current time stamp |
| 59 | l_base_dir = i_dest |
| 60 | l_timestamp = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S") |
| 61 | l_csvfile = l_base_dir + l_timestamp + ".csv" |
| 62 | |
| 63 | print "Writing data into csv file:", l_csvfile |
| 64 | |
| 65 | for testcase in collectDataObj.testData: |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 66 | # Test category : Test case type |
| 67 | l_test_name = str(testcase.parent) + ":" + str(testcase) |
| 68 | |
| 69 | # Test Result pass=0 fail=1 |
| 70 | if testcase.status == 'PASS': |
| 71 | l_test_result = 0 |
| 72 | else: |
| 73 | l_test_result = 1 |
| 74 | |
| 75 | # Data Sequence: test_start,test_end,subsys,test_type, |
| 76 | # test_result,test_name,pse_rel |
| 77 | l_data = [testcase.starttime, testcase.endtime, l_subsys, |
| 78 | l_test_type, l_test_result, l_test_name, l_pse_rel] |
| 79 | l_csvlist.append(l_data) |
| 80 | |
George Keishing | a96e27c | 2016-12-04 23:05:04 -0600 | [diff] [blame] | 81 | # Open the file and write to the CSV file |
| 82 | l_file = open(l_csvfile, "w") |
| 83 | l_writer = csv.writer(l_file, lineterminator='\n') |
| 84 | l_writer.writerows(l_csvlist) |
| 85 | l_file.close() |
George Keishing | 9fbdf79 | 2016-10-18 06:16:09 -0500 | [diff] [blame] | 86 | |
| 87 | |
| 88 | def usage(): |
| 89 | name = 'gen_csv_results.py' |
| 90 | print 'Usage: ' |
| 91 | print name, '-s <source path> -d <destination path>' |
| 92 | print '\t-s | --source= : output.xml robot test result file path' |
| 93 | print '\t-d | --dest= : Where the *.csv file will be generated' |
| 94 | sys.exit() |
| 95 | |
| 96 | |
| 97 | def main(argv): |
| 98 | |
| 99 | source = '' |
| 100 | dest = '' |
| 101 | try: |
| 102 | opts, args = getopt.getopt(argv, "h:s:d:", ["source=", "dest="]) |
| 103 | except getopt.GetoptError: |
| 104 | usage() |
| 105 | |
| 106 | for opt, arg in opts: |
| 107 | if opt == '-h': |
| 108 | usage() |
| 109 | elif opt in ("-s", "--source"): |
| 110 | source = arg |
| 111 | elif opt in ("-d", "--dest"): |
| 112 | dest = arg |
| 113 | |
| 114 | if source == '': |
| 115 | usage() |
| 116 | print 'ERROR: Provide input file path to robot generated output.xml ' |
| 117 | |
| 118 | if dest == '': |
| 119 | usage() |
| 120 | print 'ERROR: Destination directory where *.csv file will be generated' |
| 121 | |
| 122 | parse_output_xml(source, dest) |
| 123 | |
| 124 | if __name__ == "__main__": |
| 125 | main(sys.argv[1:]) |