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