FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ram54288 0:dbad57390bd1 1 #! python3
ram54288 0:dbad57390bd1 2 # ==========================================
ram54288 0:dbad57390bd1 3 # Unity Project - A Test Framework for C
ram54288 0:dbad57390bd1 4 # Copyright (c) 2015 Alexander Mueller / XelaRellum@web.de
ram54288 0:dbad57390bd1 5 # [Released under MIT License. Please refer to license.txt for details]
ram54288 0:dbad57390bd1 6 # Based on the ruby script by Mike Karlesky, Mark VanderVoord, Greg Williams
ram54288 0:dbad57390bd1 7 # ==========================================
ram54288 0:dbad57390bd1 8 import sys
ram54288 0:dbad57390bd1 9 import os
ram54288 0:dbad57390bd1 10 import re
ram54288 0:dbad57390bd1 11 from glob import glob
ram54288 0:dbad57390bd1 12
ram54288 0:dbad57390bd1 13 class UnityTestSummary:
ram54288 0:dbad57390bd1 14 def __init__(self):
ram54288 0:dbad57390bd1 15 self.report = ''
ram54288 0:dbad57390bd1 16 self.total_tests = 0
ram54288 0:dbad57390bd1 17 self.failures = 0
ram54288 0:dbad57390bd1 18 self.ignored = 0
ram54288 0:dbad57390bd1 19
ram54288 0:dbad57390bd1 20 def run(self):
ram54288 0:dbad57390bd1 21 # Clean up result file names
ram54288 0:dbad57390bd1 22 results = []
ram54288 0:dbad57390bd1 23 for target in self.targets:
ram54288 0:dbad57390bd1 24 results.append(target.replace('\\', '/'))
ram54288 0:dbad57390bd1 25
ram54288 0:dbad57390bd1 26 # Dig through each result file, looking for details on pass/fail:
ram54288 0:dbad57390bd1 27 failure_output = []
ram54288 0:dbad57390bd1 28 ignore_output = []
ram54288 0:dbad57390bd1 29
ram54288 0:dbad57390bd1 30 for result_file in results:
ram54288 0:dbad57390bd1 31 lines = list(map(lambda line: line.rstrip(), open(result_file, "r").read().split('\n')))
ram54288 0:dbad57390bd1 32 if len(lines) == 0:
ram54288 0:dbad57390bd1 33 raise Exception("Empty test result file: %s" % result_file)
ram54288 0:dbad57390bd1 34
ram54288 0:dbad57390bd1 35 details = self.get_details(result_file, lines)
ram54288 0:dbad57390bd1 36 failures = details['failures']
ram54288 0:dbad57390bd1 37 ignores = details['ignores']
ram54288 0:dbad57390bd1 38 if len(failures) > 0: failure_output.append('\n'.join(failures))
ram54288 0:dbad57390bd1 39 if len(ignores) > 0: ignore_output.append('n'.join(ignores))
ram54288 0:dbad57390bd1 40 tests,failures,ignored = self.parse_test_summary('\n'.join(lines))
ram54288 0:dbad57390bd1 41 self.total_tests += tests
ram54288 0:dbad57390bd1 42 self.failures += failures
ram54288 0:dbad57390bd1 43 self.ignored += ignored
ram54288 0:dbad57390bd1 44
ram54288 0:dbad57390bd1 45 if self.ignored > 0:
ram54288 0:dbad57390bd1 46 self.report += "\n"
ram54288 0:dbad57390bd1 47 self.report += "--------------------------\n"
ram54288 0:dbad57390bd1 48 self.report += "UNITY IGNORED TEST SUMMARY\n"
ram54288 0:dbad57390bd1 49 self.report += "--------------------------\n"
ram54288 0:dbad57390bd1 50 self.report += "\n".join(ignore_output)
ram54288 0:dbad57390bd1 51
ram54288 0:dbad57390bd1 52 if self.failures > 0:
ram54288 0:dbad57390bd1 53 self.report += "\n"
ram54288 0:dbad57390bd1 54 self.report += "--------------------------\n"
ram54288 0:dbad57390bd1 55 self.report += "UNITY FAILED TEST SUMMARY\n"
ram54288 0:dbad57390bd1 56 self.report += "--------------------------\n"
ram54288 0:dbad57390bd1 57 self.report += '\n'.join(failure_output)
ram54288 0:dbad57390bd1 58
ram54288 0:dbad57390bd1 59 self.report += "\n"
ram54288 0:dbad57390bd1 60 self.report += "--------------------------\n"
ram54288 0:dbad57390bd1 61 self.report += "OVERALL UNITY TEST SUMMARY\n"
ram54288 0:dbad57390bd1 62 self.report += "--------------------------\n"
ram54288 0:dbad57390bd1 63 self.report += "{total_tests} TOTAL TESTS {failures} TOTAL FAILURES {ignored} IGNORED\n".format(total_tests = self.total_tests, failures=self.failures, ignored=self.ignored)
ram54288 0:dbad57390bd1 64 self.report += "\n"
ram54288 0:dbad57390bd1 65
ram54288 0:dbad57390bd1 66 return self.report
ram54288 0:dbad57390bd1 67
ram54288 0:dbad57390bd1 68 def set_targets(self, target_array):
ram54288 0:dbad57390bd1 69 self.targets = target_array
ram54288 0:dbad57390bd1 70
ram54288 0:dbad57390bd1 71 def set_root_path(self, path):
ram54288 0:dbad57390bd1 72 self.root = path
ram54288 0:dbad57390bd1 73
ram54288 0:dbad57390bd1 74 def usage(self, err_msg=None):
ram54288 0:dbad57390bd1 75 print("\nERROR: ")
ram54288 0:dbad57390bd1 76 if err_msg:
ram54288 0:dbad57390bd1 77 print(err_msg)
ram54288 0:dbad57390bd1 78 print("\nUsage: unity_test_summary.py result_file_directory/ root_path/")
ram54288 0:dbad57390bd1 79 print(" result_file_directory - The location of your results files.")
ram54288 0:dbad57390bd1 80 print(" Defaults to current directory if not specified.")
ram54288 0:dbad57390bd1 81 print(" Should end in / if specified.")
ram54288 0:dbad57390bd1 82 print(" root_path - Helpful for producing more verbose output if using relative paths.")
ram54288 0:dbad57390bd1 83 sys.exit(1)
ram54288 0:dbad57390bd1 84
ram54288 0:dbad57390bd1 85 def get_details(self, result_file, lines):
ram54288 0:dbad57390bd1 86 results = { 'failures': [], 'ignores': [], 'successes': [] }
ram54288 0:dbad57390bd1 87 for line in lines:
ram54288 0:dbad57390bd1 88 parts = line.split(':')
ram54288 0:dbad57390bd1 89 if len(parts) != 5:
ram54288 0:dbad57390bd1 90 continue
ram54288 0:dbad57390bd1 91 src_file,src_line,test_name,status,msg = parts
ram54288 0:dbad57390bd1 92 if len(self.root) > 0:
ram54288 0:dbad57390bd1 93 line_out = "%s%s" % (self.root, line)
ram54288 0:dbad57390bd1 94 else:
ram54288 0:dbad57390bd1 95 line_out = line
ram54288 0:dbad57390bd1 96 if status == 'IGNORE':
ram54288 0:dbad57390bd1 97 results['ignores'].append(line_out)
ram54288 0:dbad57390bd1 98 elif status == 'FAIL':
ram54288 0:dbad57390bd1 99 results['failures'].append(line_out)
ram54288 0:dbad57390bd1 100 elif status == 'PASS':
ram54288 0:dbad57390bd1 101 results['successes'].append(line_out)
ram54288 0:dbad57390bd1 102 return results
ram54288 0:dbad57390bd1 103
ram54288 0:dbad57390bd1 104 def parse_test_summary(self, summary):
ram54288 0:dbad57390bd1 105 m = re.search(r"([0-9]+) Tests ([0-9]+) Failures ([0-9]+) Ignored", summary)
ram54288 0:dbad57390bd1 106 if not m:
ram54288 0:dbad57390bd1 107 raise Exception("Couldn't parse test results: %s" % summary)
ram54288 0:dbad57390bd1 108
ram54288 0:dbad57390bd1 109 return int(m.group(1)), int(m.group(2)), int(m.group(3))
ram54288 0:dbad57390bd1 110
ram54288 0:dbad57390bd1 111
ram54288 0:dbad57390bd1 112 if __name__ == '__main__':
ram54288 0:dbad57390bd1 113 uts = UnityTestSummary()
ram54288 0:dbad57390bd1 114 try:
ram54288 0:dbad57390bd1 115 #look in the specified or current directory for result files
ram54288 0:dbad57390bd1 116 if len(sys.argv) > 1:
ram54288 0:dbad57390bd1 117 targets_dir = sys.argv[1]
ram54288 0:dbad57390bd1 118 else:
ram54288 0:dbad57390bd1 119 targets_dir = './'
ram54288 0:dbad57390bd1 120 targets = list(map(lambda x: x.replace('\\', '/'), glob(targets_dir + '*.test*')))
ram54288 0:dbad57390bd1 121 if len(targets) == 0:
ram54288 0:dbad57390bd1 122 raise Exception("No *.testpass or *.testfail files found in '%s'" % targets_dir)
ram54288 0:dbad57390bd1 123 uts.set_targets(targets)
ram54288 0:dbad57390bd1 124
ram54288 0:dbad57390bd1 125 #set the root path
ram54288 0:dbad57390bd1 126 if len(sys.argv) > 2:
ram54288 0:dbad57390bd1 127 root_path = sys.argv[2]
ram54288 0:dbad57390bd1 128 else:
ram54288 0:dbad57390bd1 129 root_path = os.path.split(__file__)[0]
ram54288 0:dbad57390bd1 130 uts.set_root_path(root_path)
ram54288 0:dbad57390bd1 131
ram54288 0:dbad57390bd1 132 #run the summarizer
ram54288 0:dbad57390bd1 133 print(uts.run())
ram54288 0:dbad57390bd1 134 except Exception as e:
ram54288 0:dbad57390bd1 135 uts.usage(e)