Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_exporters.py 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. """
  2. mbed SDK
  3. Copyright (c) 2011-2014 ARM Limited
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. Author: Przemyslaw Wirkus <[email protected]>
  14. """
  15. from workspace_tools.utils import construct_enum
  16. ResultExporterType = construct_enum(HTML='Html_Exporter',
  17. JUNIT='JUnit_Exporter')
  18. class ReportExporter():
  19. """ Class exports extended test result Python data structure to
  20. different formats like HTML, JUnit XML.
  21. Parameter 'test_result_ext' format:
  22. u'uARM': { u'LPC1768': { 'MBED_2': { 0: { 'copy_method': 'shutils.copy()',
  23. 'duration': 20,
  24. 'elapsed_time': 1.7929999828338623,
  25. 'single_test_output': 'Host test instrumentation on ...\r\n',
  26. 'single_test_result': 'OK',
  27. 'target_name': u'LPC1768',
  28. 'test_description': 'stdio',
  29. 'test_id': u'MBED_2',
  30. 'toolchain_name': u'uARM'}},
  31. """
  32. CSS_STYLE = """<style>
  33. .name{
  34. border: 1px solid;
  35. border-radius: 25px;
  36. width: 100px;
  37. }
  38. .tooltip{
  39. position:absolute;
  40. background-color: #F5DA81;
  41. display:none;
  42. }
  43. </style>
  44. """
  45. JAVASCRIPT = """
  46. <script type="text/javascript">
  47. function show (elem) {
  48. elem.style.display = "block";
  49. }
  50. function hide (elem) {
  51. elem.style.display = "";
  52. }
  53. </script>
  54. """
  55. def __init__(self, result_exporter_type):
  56. self.result_exporter_type = result_exporter_type
  57. def report(self, test_summary_ext, test_suite_properties=None):
  58. """ Invokes report depending on exporter_type set in constructor
  59. """
  60. if self.result_exporter_type == ResultExporterType.HTML:
  61. # HTML exporter
  62. return self.exporter_html(test_summary_ext, test_suite_properties)
  63. elif self.result_exporter_type == ResultExporterType.JUNIT:
  64. # JUNIT exporter
  65. return self.exporter_junit(test_summary_ext, test_suite_properties)
  66. return None
  67. def report_to_file(self, test_summary_ext, file_name, test_suite_properties=None):
  68. """ Stores report to specified file
  69. """
  70. report = self.report(test_summary_ext, test_suite_properties=test_suite_properties)
  71. if report is not None:
  72. with open(file_name, 'w') as f:
  73. f.write(report)
  74. def get_tooltip_name(self, toolchain, target, test_id, loop_no):
  75. """ Generate simple unique tool-tip name which can be used.
  76. For example as HTML <div> section id attribute.
  77. """
  78. return "target_test_%s_%s_%s_%d"% (toolchain.lower(), target.lower(), test_id.lower(), loop_no)
  79. def get_result_div_sections(self, test, test_no):
  80. """ Generates separate <dvi> sections which contains test results output.
  81. """
  82. RESULT_COLORS = {'OK' : 'LimeGreen',
  83. 'FAIL' : 'Orange',
  84. 'ERROR' : 'LightCoral',}
  85. tooltip_name = self.get_tooltip_name(test['toolchain_name'], test['target_name'], test['test_id'], test_no)
  86. background_color = RESULT_COLORS[test['single_test_result'] if test['single_test_result'] in RESULT_COLORS else 'ERROR']
  87. result_div_style = "background-color: %s"% background_color
  88. result = """<div class="name" style="%s" onmouseover="show(%s)" onmouseout="hide(%s)">
  89. <center>%s</center>
  90. <div class = "tooltip" id= "%s">
  91. <b>%s</b> in <b>%.2f sec</b><br />
  92. <hr />
  93. <small>
  94. %s
  95. </small>
  96. </div>
  97. </div>
  98. """% (result_div_style,
  99. tooltip_name,
  100. tooltip_name,
  101. test['single_test_result'],
  102. tooltip_name,
  103. test['test_description'],
  104. test['elapsed_time'],
  105. test['single_test_output'].replace('\n', '<br />'))
  106. return result
  107. def get_result_tree(self, test_results):
  108. """ If test was run in a loop (we got few results from the same test)
  109. we will show it in a column to see all results.
  110. This function produces HTML table with corresponding results.
  111. """
  112. result = '<table>'
  113. test_ids = sorted(test_results.keys())
  114. for test_no in test_ids:
  115. test = test_results[test_no]
  116. result += """<tr>
  117. <td valign="top">%s</td>
  118. </tr>"""% self.get_result_div_sections(test, test_no)
  119. result += '</table>'
  120. return result
  121. def get_all_unique_test_ids(self, test_result_ext):
  122. """ Gets all unique test ids from all ran tests.
  123. We need this to create complete list of all test ran.
  124. """
  125. result = []
  126. toolchains = test_result_ext.keys()
  127. for toolchain in toolchains:
  128. targets = test_result_ext[toolchain].keys()
  129. for target in targets:
  130. tests = test_result_ext[toolchain][target].keys()
  131. result.extend(tests)
  132. return sorted(list(set(result)))
  133. #
  134. # Exporters functions
  135. #
  136. def exporter_html(self, test_result_ext, test_suite_properties=None):
  137. """ Export test results in proprietary html format.
  138. """
  139. result = """<html>
  140. <head>
  141. <title>mbed SDK test suite test result report</title>
  142. %s
  143. %s
  144. </head>
  145. <body>
  146. """% (self.CSS_STYLE, self.JAVASCRIPT)
  147. unique_test_ids = self.get_all_unique_test_ids(test_result_ext)
  148. toolchains = sorted(test_result_ext.keys())
  149. result += '<table><tr>'
  150. for toolchain in toolchains:
  151. targets = sorted(test_result_ext[toolchain].keys())
  152. for target in targets:
  153. result += '<td></td>'
  154. result += '<td></td>'
  155. tests = sorted(test_result_ext[toolchain][target].keys())
  156. for test in unique_test_ids:
  157. result += """<td align="center">%s</td>"""% test
  158. result += """</tr>
  159. <tr>
  160. <td valign="center">%s</td>
  161. <td valign="center"><b>%s</b></td>
  162. """% (toolchain, target)
  163. for test in unique_test_ids:
  164. test_result = self.get_result_tree(test_result_ext[toolchain][target][test]) if test in tests else ''
  165. result += '<td>%s</td>'% (test_result)
  166. result += '</tr>'
  167. result += '</table>'
  168. result += '</body></html>'
  169. return result
  170. def exporter_junit(self, test_result_ext, test_suite_properties=None):
  171. """ Export test results in JUnit XML compliant format
  172. """
  173. from junit_xml import TestSuite, TestCase
  174. test_suites = []
  175. test_cases = []
  176. toolchains = sorted(test_result_ext.keys())
  177. for toolchain in toolchains:
  178. targets = sorted(test_result_ext[toolchain].keys())
  179. for target in targets:
  180. test_cases = []
  181. tests = sorted(test_result_ext[toolchain][target].keys())
  182. for test in tests:
  183. test_results = test_result_ext[toolchain][target][test]
  184. test_ids = sorted(test_results.keys())
  185. for test_no in test_ids:
  186. test_result = test_results[test_no]
  187. name = test_result['test_description']
  188. classname = 'test.%s.%s.%s'% (target, toolchain, test_result['test_id'])
  189. elapsed_sec = test_result['elapsed_time']
  190. _stdout = test_result['single_test_output']
  191. _stderr = ''
  192. # Test case
  193. tc = TestCase(name, classname, elapsed_sec, _stdout, _stderr)
  194. # Test case extra failure / error info
  195. if test_result['single_test_result'] == 'FAIL':
  196. message = test_result['single_test_result']
  197. tc.add_failure_info(message, _stdout)
  198. elif test_result['single_test_result'] != 'OK':
  199. message = test_result['single_test_result']
  200. tc.add_error_info(message, _stdout)
  201. test_cases.append(tc)
  202. ts = TestSuite("test.suite.%s.%s"% (target, toolchain), test_cases, properties=test_suite_properties[target][toolchain])
  203. test_suites.append(ts)
  204. return TestSuite.to_xml_string(test_suites)