2 # ==========================================
3 # Unity Project - A Test Framework for C
4 # Copyright (c) 2015 Alexander Mueller / XelaRellum@web.de
5 # [Released under MIT License. Please refer to license.txt for details]
6 # Based on the ruby script by Mike Karlesky, Mark VanderVoord, Greg Williams
7 # ==========================================
13 class UnityTestSummary:
21 # Clean up result file names
23 for target in self.targets:
24 results.append(target.replace('\\', '/'))
26 # Dig through each result file, looking for details on pass/fail:
30 for result_file in results:
31 lines = list(map(lambda line: line.rstrip(), open(result_file, "r").read().split('\n')))
33 raise Exception("Empty test result file: %s" % result_file)
35 details = self.get_details(result_file, lines)
36 failures = details['failures']
37 ignores = details['ignores']
38 if len(failures) > 0: failure_output.append('\n'.join(failures))
39 if len(ignores) > 0: ignore_output.append('n'.join(ignores))
40 tests,failures,ignored = self.parse_test_summary('\n'.join(lines))
41 self.total_tests += tests
42 self.failures += failures
43 self.ignored += ignored
47 self.report += "--------------------------\n"
48 self.report += "UNITY IGNORED TEST SUMMARY\n"
49 self.report += "--------------------------\n"
50 self.report += "\n".join(ignore_output)
54 self.report += "--------------------------\n"
55 self.report += "UNITY FAILED TEST SUMMARY\n"
56 self.report += "--------------------------\n"
57 self.report += '\n'.join(failure_output)
60 self.report += "--------------------------\n"
61 self.report += "OVERALL UNITY TEST SUMMARY\n"
62 self.report += "--------------------------\n"
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)
68 def set_targets(self, target_array):
69 self.targets = target_array
71 def set_root_path(self, path):
74 def usage(self, err_msg=None):
78 print("\nUsage: unity_test_summary.rb result_file_directory/ root_path/")
79 print(" result_file_directory - The location of your results files.")
80 print(" Defaults to current directory if not specified.")
81 print(" Should end in / if specified.")
82 print(" root_path - Helpful for producing more verbose output if using relative paths.")
85 def get_details(self, result_file, lines):
86 results = { 'failures': [], 'ignores': [], 'successes': [] }
88 parts = line.split(':')
91 src_file,src_line,test_name,status,msg = parts
92 if len(self.root) > 0:
93 line_out = "%s%s" % (self.root, line)
96 if status == 'IGNORE':
97 results['ignores'].append(line_out)
98 elif status == 'FAIL':
99 results['failures'].append(line_out)
100 elif status == 'PASS':
101 results['successes'].append(line_out)
104 def parse_test_summary(self, summary):
105 m = re.search(r"([0-9]+) Tests ([0-9]+) Failures ([0-9]+) Ignored", summary)
107 raise Exception("Couldn't parse test results: %s" % summary)
109 return int(m.group(1)), int(m.group(2)), int(m.group(3))
112 if __name__ == '__main__':
113 uts = UnityTestSummary()
115 #look in the specified or current directory for result files
116 if len(sys.argv) > 1:
117 targets_dir = sys.argv[1]
120 targets = list(map(lambda x: x.replace('\\', '/'), glob(targets_dir + '*.test*')))
121 if len(targets) == 0:
122 raise Exception("No *.testpass or *.testfail files found in '%s'" % targets_dir)
123 uts.set_targets(targets)
126 if len(sys.argv) > 2:
127 root_path = sys.argv[2]
129 root_path = os.path.split(__file__)[0]
130 uts.set_root_path(root_path)
134 except Exception as e: