forked from arduino/mbed-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_exporters.py
416 lines (366 loc) · 17.9 KB
/
test_exporters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
"""
mbed SDK
Copyright (c) 2011-2014 ARM Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Przemyslaw Wirkus <[email protected]>
"""
from tools.utils import construct_enum, mkdir
from prettytable import PrettyTable
import os
ResultExporterType = construct_enum(HTML='Html_Exporter',
JUNIT='JUnit_Exporter',
JUNIT_OPER='JUnit_Exporter_Interoperability',
BUILD='Build_Exporter',
TEXT='Text_Exporter',
PRINT='Print_Exporter')
class ReportExporter(object):
""" Class exports extended test result Python data structure to
different formats like HTML, JUnit XML.
Parameter 'test_result_ext' format:
u'uARM': { u'LPC1768': { 'MBED_2': { 0: { 'copy_method': 'shutils.copy()',
'duration': 20,
'elapsed_time': 1.7929999828338623,
'output': 'Host test instrumentation on ...\r\n',
'result': 'OK',
'target_name': u'LPC1768',
'description': 'stdio',
'id': u'MBED_2',
'toolchain_name': u'uARM'}},
"""
CSS_STYLE = """<style>
.name{
border: 1px solid;
border-radius: 25px;
width: 100px;
}
.tooltip{
position:absolute;
background-color: #F5DA81;
display:none;
}
</style>
"""
JAVASCRIPT = """
<script type="text/javascript">
function show (elem) {
elem.style.display = "block";
}
function hide (elem) {
elem.style.display = "";
}
</script>
"""
def __init__(self, result_exporter_type, package="test"):
self.result_exporter_type = result_exporter_type
self.package = package
def report(self, test_summary_ext, test_suite_properties=None,
print_log_for_failures=True):
""" Invokes report depending on exporter_type set in constructor
"""
if self.result_exporter_type == ResultExporterType.HTML:
# HTML exporter
return self.exporter_html(test_summary_ext, test_suite_properties)
elif self.result_exporter_type == ResultExporterType.JUNIT:
# JUNIT exporter for results from test suite
return self.exporter_junit(test_summary_ext, test_suite_properties)
elif self.result_exporter_type == ResultExporterType.JUNIT_OPER:
# JUNIT exporter for interoperability test
return self.exporter_junit_ioper(test_summary_ext, test_suite_properties)
elif self.result_exporter_type == ResultExporterType.PRINT:
# JUNIT exporter for interoperability test
return self.exporter_print(test_summary_ext, print_log_for_failures=print_log_for_failures)
elif self.result_exporter_type == ResultExporterType.TEXT:
return self.exporter_text(test_summary_ext)
return None
def report_to_file(self, test_summary_ext, file_name, test_suite_properties=None):
""" Stores report to specified file
"""
report = self.report(test_summary_ext, test_suite_properties=test_suite_properties)
self.write_to_file(report, file_name)
def write_to_file(self, report, file_name):
if report is not None:
dirname = os.path.dirname(file_name)
if dirname:
mkdir(dirname)
with open(file_name, 'w') as f:
f.write(report)
def get_tooltip_name(self, toolchain, target, test_id, loop_no):
""" Generate simple unique tool-tip name which can be used.
For example as HTML <div> section id attribute.
"""
return "target_test_%s_%s_%s_%s"% (toolchain.lower(), target.lower(), test_id.lower(), loop_no)
def get_result_div_sections(self, test, test_no):
""" Generates separate <DIV> sections which contains test results output.
"""
RESULT_COLORS = {'OK': 'LimeGreen',
'FAIL': 'Orange',
'ERROR': 'LightCoral',
'OTHER': 'LightGray',
}
tooltip_name = self.get_tooltip_name(test['toolchain_name'], test['target_name'], test['id'], test_no)
background_color = RESULT_COLORS[test['result'] if test['result'] in RESULT_COLORS else 'OTHER']
result_div_style = "background-color: %s"% background_color
result = """<div class="name" style="%s" onmouseover="show(%s)" onmouseout="hide(%s)">
<center>%s</center>
<div class = "tooltip" id= "%s">
<b>%s</b><br />
<hr />
<b>%s</b> in <b>%.2f sec</b><br />
<hr />
<small>
%s
</small>
</div>
</div>
"""% (result_div_style,
tooltip_name,
tooltip_name,
test['result'],
tooltip_name,
test['target_name_unique'],
test['description'],
test['elapsed_time'],
test['output'].replace('\n', '<br />'))
return result
def get_result_tree(self, test_results):
""" If test was run in a loop (we got few results from the same test)
we will show it in a column to see all results.
This function produces HTML table with corresponding results.
"""
result = ''
for i, test_result in enumerate(test_results):
result += '<table>'
test_ids = sorted(test_result.keys())
for test_no in test_ids:
test = test_result[test_no]
result += """<tr>
<td valign="top">%s</td>
</tr>"""% self.get_result_div_sections(test, "%d_%d" % (test_no, i))
result += '</table>'
return result
def get_all_unique_test_ids(self, test_result_ext):
""" Gets all unique test ids from all ran tests.
We need this to create complete list of all test ran.
"""
result = []
targets = test_result_ext.keys()
for target in targets:
toolchains = test_result_ext[target].keys()
for toolchain in toolchains:
tests = test_result_ext[target][toolchain].keys()
result.extend(tests)
return sorted(list(set(result)))
#
# Exporters functions
#
def exporter_html(self, test_result_ext, test_suite_properties=None):
""" Export test results in proprietary HTML format.
"""
result = """<html>
<head>
<title>mbed SDK test suite test result report</title>
%s
%s
</head>
<body>
"""% (self.CSS_STYLE, self.JAVASCRIPT)
unique_test_ids = self.get_all_unique_test_ids(test_result_ext)
targets = sorted(test_result_ext.keys())
result += '<table>'
for target in targets:
toolchains = sorted(test_result_ext[target].keys())
for toolchain in toolchains:
result += '<tr>'
result += '<td></td>'
result += '<td></td>'
tests = sorted(test_result_ext[target][toolchain].keys())
for test in unique_test_ids:
result += """<td align="center">%s</td>"""% test
result += """</tr>
<tr>
<td valign="center">%s</td>
<td valign="center"><b>%s</b></td>
"""% (toolchain, target)
for test in unique_test_ids:
test_result = self.get_result_tree(test_result_ext[target][toolchain][test]) if test in tests else ''
result += '<td>%s</td>'% (test_result)
result += '</tr>'
result += '</table>'
result += '</body></html>'
return result
def exporter_junit_ioper(self, test_result_ext, test_suite_properties=None):
from junit_xml import TestSuite, TestCase
test_suites = []
test_cases = []
for platform in sorted(test_result_ext.keys()):
# {platform : ['Platform', 'Result', 'Scope', 'Description'])
test_cases = []
for tr_result in test_result_ext[platform]:
result, name, scope, description = tr_result
classname = 'test.ioper.%s.%s.%s' % (platform, name, scope)
elapsed_sec = 0
_stdout = description
_stderr = ''
# Test case
tc = TestCase(name, classname, elapsed_sec, _stdout, _stderr)
# Test case extra failure / error info
if result == 'FAIL':
tc.add_failure_info(description, _stdout)
elif result == 'ERROR':
tc.add_error_info(description, _stdout)
elif result == 'SKIP' or result == 'NOT_SUPPORTED':
tc.add_skipped_info(description, _stdout)
test_cases.append(tc)
ts = TestSuite("test.suite.ioper.%s" % (platform), test_cases)
test_suites.append(ts)
return TestSuite.to_xml_string(test_suites)
def exporter_junit(self, test_result_ext, test_suite_properties=None):
""" Export test results in JUnit XML compliant format
"""
from junit_xml import TestSuite, TestCase
test_suites = []
test_cases = []
targets = sorted(test_result_ext.keys())
for target in targets:
toolchains = sorted(test_result_ext[target].keys())
for toolchain in toolchains:
test_cases = []
tests = sorted(test_result_ext[target][toolchain].keys())
for test in tests:
test_results = test_result_ext[target][toolchain][test]
for test_res in test_results:
test_ids = sorted(test_res.keys())
for test_no in test_ids:
test_result = test_res[test_no]
name = test_result['description']
classname = '%s.%s.%s.%s'% (self.package, target, toolchain, test_result['id'])
elapsed_sec = test_result['elapsed_time']
_stdout = test_result['output']
if 'target_name_unique' in test_result:
_stderr = test_result['target_name_unique']
else:
_stderr = test_result['target_name']
# Test case
tc = TestCase(name, classname, elapsed_sec, _stdout, _stderr)
# Test case extra failure / error info
message = test_result['result']
if test_result['result'] == 'FAIL':
tc.add_failure_info(message, _stdout)
elif test_result['result'] == 'SKIP' or test_result["result"] == 'NOT_SUPPORTED':
tc.add_skipped_info(message, _stdout)
elif test_result['result'] != 'OK':
tc.add_error_info(message, _stdout)
test_cases.append(tc)
ts = TestSuite("test.suite.%s.%s"% (target, toolchain), test_cases, properties=test_suite_properties[target][toolchain])
test_suites.append(ts)
return TestSuite.to_xml_string(test_suites)
def exporter_print_helper(self, array, print_log=False):
for item in array:
print(" * %s::%s::%s" % (item["target_name"],
item["toolchain_name"],
item["id"]))
if print_log:
log_lines = item["output"].split("\n")
for log_line in log_lines:
print(" %s" % log_line)
def exporter_print(self, test_result_ext, print_log_for_failures=False):
""" Export test results in print format.
"""
failures = []
skips = []
successes = []
unique_test_ids = self.get_all_unique_test_ids(test_result_ext)
targets = sorted(test_result_ext.keys())
for target in targets:
toolchains = sorted(test_result_ext[target].keys())
for toolchain in toolchains:
tests = sorted(test_result_ext[target][toolchain].keys())
for test in tests:
test_runs = test_result_ext[target][toolchain][test]
for test_runner in test_runs:
#test_run = test_result_ext[target][toolchain][test][test_run_number][0]
test_run = test_runner[0]
if "result" in test_run:
if test_run["result"] == "FAIL":
failures.append(test_run)
elif test_run["result"] == "SKIP" or test_run["result"] == "NOT_SUPPORTED":
skips.append(test_run)
elif test_run["result"] == "OK":
successes.append(test_run)
else:
raise Exception("Unhandled result type: %s" % (test_run["result"]))
else:
raise Exception("'test_run' did not have a 'result' value")
if successes:
print("\n\nBuild successes:")
self.exporter_print_helper(successes)
if skips:
print("\n\nBuild skips:")
self.exporter_print_helper(skips)
if failures:
print("\n\nBuild failures:")
self.exporter_print_helper(failures, print_log=print_log_for_failures)
return False
else:
return True
def exporter_text(self, test_result_ext):
""" Prints well-formed summary with results (SQL table like)
table shows target x test results matrix across
"""
success_code = 0 # Success code that can be leter returned to
# Pretty table package is used to print results
pt = PrettyTable(["Result", "Target", "Toolchain", "Test ID", "Test Description",
"Elapsed Time", "Timeout"])
pt.align["Result"] = "l" # Left align
pt.align["Target"] = "l" # Left align
pt.align["Toolchain"] = "l" # Left align
pt.align["Test ID"] = "l" # Left align
pt.align["Test Description"] = "l" # Left align
pt.padding_width = 1 # One space between column edges and contents (default)
result_dict = {"OK" : 0,
"FAIL" : 0,
"ERROR" : 0,
"UNDEF" : 0,
"IOERR_COPY" : 0,
"IOERR_DISK" : 0,
"IOERR_SERIAL" : 0,
"TIMEOUT" : 0,
"NO_IMAGE" : 0,
"MBED_ASSERT" : 0,
"BUILD_FAILED" : 0,
"NOT_SUPPORTED" : 0
}
unique_test_ids = self.get_all_unique_test_ids(test_result_ext)
targets = sorted(test_result_ext.keys())
for target in targets:
toolchains = sorted(test_result_ext[target].keys())
for toolchain in toolchains:
test_cases = []
tests = sorted(test_result_ext[target][toolchain].keys())
for test in tests:
test_results = test_result_ext[target][toolchain][test]
for test_res in test_results:
test_ids = sorted(test_res.keys())
for test_no in test_ids:
test_result = test_res[test_no]
result_dict[test_result['result']] += 1
pt.add_row([test_result['result'],
test_result['target_name'],
test_result['toolchain_name'],
test_result['id'],
test_result['description'],
test_result['elapsed_time'],
test_result['duration']])
result = pt.get_string()
result += "\n"
# Print result count
result += "Result: " + ' / '.join(['%s %s' % (value, key) for (key, value) in {k: v for k, v in result_dict.items() if v != 0}.items()])
return result