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.
7 from pylib
.base
import base_test_result
10 def GenerateResultsDict(test_run_result
):
11 """Create a results dict from |test_run_result| suitable for writing to JSON.
13 test_run_result: a base_test_result.TestRunResults object.
15 A results dict that mirrors the one generated by
16 base/test/launcher/test_results_tracker.cc:SaveSummaryAsJSON.
18 # Example json output.
25 # "disabled_tests": [],
26 # "per_iteration_data": [
30 # "status": "SUCCESS",
31 # "elapsed_time_ms": 1,
32 # "output_snippet": "",
33 # "output_snippet_base64": "",
34 # "losless_snippet": "",
39 # "status": "FAILURE",
40 # "elapsed_time_ms": 12,
41 # "output_snippet": "",
42 # "output_snippet_base64": "",
43 # "losless_snippet": "",
50 assert isinstance(test_run_result
, base_test_result
.TestRunResults
)
52 def status_as_string(s
):
53 if s
== base_test_result
.ResultType
.PASS
:
55 elif s
== base_test_result
.ResultType
.SKIP
:
57 elif s
== base_test_result
.ResultType
.FAIL
:
59 elif s
== base_test_result
.ResultType
.CRASH
:
61 elif s
== base_test_result
.ResultType
.TIMEOUT
:
63 elif s
== base_test_result
.ResultType
.UNKNOWN
:
66 def generate_iteration_data(t
):
70 'status': status_as_string(t
.GetType()),
71 'elapsed_time_ms': t
.GetDuration(),
73 'losless_snippet': '',
74 'output_snippet_base64:': '',
79 all_tests_tuple
, per_iteration_data_tuple
= zip(
80 *[(t
.GetName(), generate_iteration_data(t
))
81 for t
in test_run_result
.GetAll()])
85 'all_tests': list(all_tests_tuple
),
86 # TODO(jbudorick): Add support for disabled tests within base_test_result.
88 'per_iteration_data': list(per_iteration_data_tuple
),
92 def GenerateJsonResultsFile(test_run_result
, file_path
):
93 """Write |test_run_result| to JSON.
95 This emulates the format of the JSON emitted by
96 base/test/launcher/test_results_tracker.cc:SaveSummaryAsJSON.
99 test_run_result: a base_test_result.TestRunResults object.
100 file_path: The path to the JSON file to write.
102 with
open(file_path
, 'w') as json_result_file
:
103 json_result_file
.write(json
.dumps(GenerateResultsDict(test_run_result
)))
106 def ParseResultsFromJson(json_results
):
107 """Creates a list of BaseTestResult objects from JSON.
110 json_results: A JSON dict in the format created by
111 GenerateJsonResultsFile.
114 def string_as_status(s
):
116 return base_test_result
.ResultType
.PASS
118 return base_test_result
.ResultType
.SKIP
120 return base_test_result
.ResultType
.FAIL
122 return base_test_result
.ResultType
.CRASH
124 return base_test_result
.ResultType
.TIMEOUT
126 return base_test_result
.ResultType
.UNKNOWN
129 testsuite_runs
= json_results
['per_iteration_data']
130 for testsuite_run
in testsuite_runs
:
131 for test
, test_runs
in testsuite_run
.iteritems():
133 [base_test_result
.BaseTestResult(test
,
134 string_as_status(tr
['status']),
135 duration
=tr
['elapsed_time_ms'])
136 for tr
in test_runs
])