OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import json | |
6 import logging | |
7 | |
8 from pylib.base import base_test_result | |
9 | |
10 | |
11 def GenerateResultsDict(test_run_result): | |
klundberg
2014/11/25 18:59:49
I think it would be useful to have some unit tests
jbudorick
2014/11/25 23:32:18
Sounds like a good idea to me. Tests added.
| |
12 """Create a results dict from |test_run_result| suitable for writing to JSON. | |
13 Args: | |
14 test_run_result: a base_test_result.TestRunResults object. | |
15 Returns: | |
16 A results dict that mirrors the one generated by | |
17 base/test/launcher/test_results_tracker.cc:SaveSummaryAsJSON. | |
18 """ | |
19 assert isinstance(test_run_result, base_test_result.TestRunResults) | |
20 | |
21 def status_as_string(s): | |
22 if s == base_test_result.ResultType.PASS: | |
23 return 'SUCCESS' | |
24 elif s == base_test_result.ResultType.SKIP: | |
25 return 'SKIPPED' | |
26 elif s == base_test_result.ResultType.FAIL: | |
27 return 'FAILURE' | |
28 elif s == base_test_result.ResultType.CRASH: | |
29 return 'CRASH' | |
30 elif s == base_test_result.ResultType.TIMEOUT: | |
31 return 'TIMEOUT' | |
32 elif s == base_test_result.ResultType.UNKNOWN: | |
33 return 'UNKNOWN' | |
34 | |
35 def generate_iteration_data(t): | |
36 return { | |
37 t.GetName(): [ | |
38 { | |
39 'status': status_as_string(t.GetType()), | |
40 'elapsed_time_ms': t.GetDuration(), | |
41 'output_snippet': '', | |
42 'losless_snippet': '', | |
43 'output_snippet_base64:': '', | |
44 } | |
45 ] | |
46 } | |
47 | |
48 all_tests, per_iteration_data = zip( | |
49 *[(t.GetName(), generate_iteration_data(t)) | |
50 for t in test_run_result.GetAll()]) | |
51 | |
52 return { | |
53 'global_tags': [], | |
54 'all_tests': all_tests, | |
55 # TODO(jbudorick): Add support for disabled tests within base_test_result. | |
56 'disabled_tests': [], | |
57 'per_iteration_data': per_iteration_data, | |
58 } | |
59 | |
60 | |
61 def GenerateJsonResultsFile(test_run_result, file_path): | |
62 """Write |test_run_result| to JSON. | |
63 | |
64 This emulates the format of the JSON emitted by | |
65 base/test/launcher/test_results_tracker.cc:SaveSummaryAsJSON. | |
66 | |
67 Args: | |
68 test_run_result: a base_test_result.TestRunResults object. | |
69 file_path: The path to the JSON file to write. | |
70 """ | |
71 with open(file_path, 'w') as json_result_file: | |
72 json_result_file.write(json.dumps(GenerateResultsDict(test_run_result))) | |
73 | |
OLD | NEW |