stylize_as_junit.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #! python3
  2. # ==========================================
  3. # Fork from Unity Project - A Test Framework for C
  4. # Pull request on Gerrit in progress, the objective of this file is to be deleted when official Unity deliveries
  5. # include that modification
  6. # Copyright (c) 2015 Alexander Mueller / XelaRellum@web.de
  7. # [Released under MIT License. Please refer to license.txt for details]
  8. # ==========================================
  9. import sys
  10. import os
  11. from glob import glob
  12. import argparse
  13. from pyparsing import *
  14. from junit_xml import TestSuite, TestCase
  15. class UnityTestSummary:
  16. def __init__(self):
  17. self.report = ''
  18. self.total_tests = 0
  19. self.failures = 0
  20. self.ignored = 0
  21. self.targets = 0
  22. self.root = None
  23. self.output = None
  24. self.test_suites = dict()
  25. def run(self):
  26. # Clean up result file names
  27. results = []
  28. for target in self.targets:
  29. results.append(target.replace('\\', '/'))
  30. # Dig through each result file, looking for details on pass/fail:
  31. for result_file in results:
  32. lines = list(map(lambda line: line.rstrip(), open(result_file, "r").read().split('\n')))
  33. if len(lines) == 0:
  34. raise Exception("Empty test result file: %s" % result_file)
  35. # define an expression for your file reference
  36. entry_one = Combine(
  37. oneOf(list(alphas)) + ':/' +
  38. Word(alphanums + '_-./'))
  39. entry_two = Word(printables + ' ', excludeChars=':')
  40. entry = entry_one | entry_two
  41. delimiter = Literal(':').suppress()
  42. # Format of a result line is `[file_name]:line:test_name:RESULT[:msg]`
  43. tc_result_line = Group(ZeroOrMore(entry.setResultsName('tc_file_name'))
  44. + delimiter + entry.setResultsName('tc_line_nr')
  45. + delimiter + entry.setResultsName('tc_name')
  46. + delimiter + entry.setResultsName('tc_status') +
  47. Optional(delimiter + entry.setResultsName('tc_msg'))).setResultsName("tc_line")
  48. eol = LineEnd().suppress()
  49. sol = LineStart().suppress()
  50. blank_line = sol + eol
  51. # Format of the summary line is `# Tests # Failures # Ignored`
  52. tc_summary_line = Group(Word(nums).setResultsName("num_of_tests") + "Tests" + Word(nums).setResultsName(
  53. "num_of_fail") + "Failures" + Word(nums).setResultsName("num_of_ignore") + "Ignored").setResultsName(
  54. "tc_summary")
  55. tc_end_line = Or(Literal("FAIL"), Literal('Ok')).setResultsName("tc_result")
  56. # run it and see...
  57. pp1 = tc_result_line | Optional(tc_summary_line | tc_end_line)
  58. pp1.ignore(blank_line | OneOrMore("-"))
  59. result = list()
  60. for l in lines:
  61. result.append((pp1.parseString(l)).asDict())
  62. # delete empty results
  63. result = filter(None, result)
  64. tc_list = list()
  65. for r in result:
  66. if 'tc_line' in r:
  67. tmp_tc_line = r['tc_line']
  68. # get only the file name which will be used as the classname
  69. if 'tc_file_name' in tmp_tc_line:
  70. file_name = tmp_tc_line['tc_file_name'].split('\\').pop().split('/').pop().rsplit('.', 1)[0]
  71. else:
  72. file_name = result_file.strip("./")
  73. tmp_tc = TestCase(name=tmp_tc_line['tc_name'], classname=file_name)
  74. if 'tc_status' in tmp_tc_line:
  75. if str(tmp_tc_line['tc_status']) == 'IGNORE':
  76. if 'tc_msg' in tmp_tc_line:
  77. tmp_tc.add_skipped_info(message=tmp_tc_line['tc_msg'],
  78. output=r'[File]={0}, [Line]={1}'.format(
  79. tmp_tc_line['tc_file_name'], tmp_tc_line['tc_line_nr']))
  80. else:
  81. tmp_tc.add_skipped_info(message=" ")
  82. elif str(tmp_tc_line['tc_status']) == 'FAIL':
  83. if 'tc_msg' in tmp_tc_line:
  84. tmp_tc.add_failure_info(message=tmp_tc_line['tc_msg'],
  85. output=r'[File]={0}, [Line]={1}'.format(
  86. tmp_tc_line['tc_file_name'], tmp_tc_line['tc_line_nr']))
  87. else:
  88. tmp_tc.add_failure_info(message=" ")
  89. tc_list.append((str(result_file), tmp_tc))
  90. for k, v in tc_list:
  91. try:
  92. self.test_suites[k].append(v)
  93. except KeyError:
  94. self.test_suites[k] = [v]
  95. ts = []
  96. for suite_name in self.test_suites:
  97. ts.append(TestSuite(suite_name, self.test_suites[suite_name]))
  98. with open(self.output, 'w') as f:
  99. TestSuite.to_file(f, ts, prettyprint='True', encoding='utf-8')
  100. return self.report
  101. def set_targets(self, target_array):
  102. self.targets = target_array
  103. def set_root_path(self, path):
  104. self.root = path
  105. def set_output(self, output):
  106. self.output = output
  107. if __name__ == '__main__':
  108. uts = UnityTestSummary()
  109. parser = argparse.ArgumentParser(description=
  110. """Takes as input the collection of *.testpass and *.testfail result
  111. files, and converts them to a JUnit formatted XML.""")
  112. parser.add_argument('targets_dir', metavar='result_file_directory',
  113. type=str, nargs='?', default='./',
  114. help="""The location of your results files.
  115. Defaults to current directory if not specified.""")
  116. parser.add_argument('root_path', nargs='?',
  117. default='os.path.split(__file__)[0]',
  118. help="""Helpful for producing more verbose output if
  119. using relative paths.""")
  120. parser.add_argument('--output', '-o', type=str, default="result.xml",
  121. help="""The name of the JUnit-formatted file (XML).""")
  122. args = parser.parse_args()
  123. if args.targets_dir[-1] != '/':
  124. args.targets_dir+='/'
  125. targets = list(map(lambda x: x.replace('\\', '/'), glob(args.targets_dir + '*.test*')))
  126. if len(targets) == 0:
  127. raise Exception("No *.testpass or *.testfail files found in '%s'" % args.targets_dir)
  128. uts.set_targets(targets)
  129. # set the root path
  130. uts.set_root_path(args.root_path)
  131. # set output
  132. uts.set_output(args.output)
  133. # run the summarizer
  134. print(uts.run())