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.
8 from pylib
.base
import base_test_result
11 def GenerateResultsDict(test_run_result
):
12 """Create a results dict from |test_run_result| suitable for writing to JSON.
14 test_run_result: a base_test_result.TestRunResults object.
16 A results dict that mirrors the one generated by
17 base/test/launcher/test_results_tracker.cc:SaveSummaryAsJSON.
19 # Example json output.
26 # "disabled_tests": [],
27 # "per_iteration_data": [
31 # "status": "SUCCESS",
32 # "elapsed_time_ms": 1,
33 # "output_snippet": "",
34 # "output_snippet_base64": "",
35 # "losless_snippet": "",
40 # "status": "FAILURE",
41 # "elapsed_time_ms": 12,
42 # "output_snippet": "",
43 # "output_snippet_base64": "",
44 # "losless_snippet": "",
51 assert isinstance(test_run_result
, base_test_result
.TestRunResults
)
53 def status_as_string(s
):
54 if s
== base_test_result
.ResultType
.PASS
:
56 elif s
== base_test_result
.ResultType
.SKIP
:
58 elif s
== base_test_result
.ResultType
.FAIL
:
60 elif s
== base_test_result
.ResultType
.CRASH
:
62 elif s
== base_test_result
.ResultType
.TIMEOUT
:
64 elif s
== base_test_result
.ResultType
.UNKNOWN
:
67 def generate_iteration_data(t
):
71 'status': status_as_string(t
.GetType()),
72 'elapsed_time_ms': t
.GetDuration(),
74 'losless_snippet': '',
75 'output_snippet_base64:': '',
80 all_tests_tuple
, per_iteration_data_tuple
= zip(
81 *[(t
.GetName(), generate_iteration_data(t
))
82 for t
in test_run_result
.GetAll()])
86 'all_tests': list(all_tests_tuple
),
87 # TODO(jbudorick): Add support for disabled tests within base_test_result.
89 'per_iteration_data': list(per_iteration_data_tuple
),
93 def GenerateJsonResultsFile(test_run_result
, file_path
):
94 """Write |test_run_result| to JSON.
96 This emulates the format of the JSON emitted by
97 base/test/launcher/test_results_tracker.cc:SaveSummaryAsJSON.
100 test_run_result: a base_test_result.TestRunResults object.
101 file_path: The path to the JSON file to write.
103 with
open(file_path
, 'w') as json_result_file
:
104 json_result_file
.write(json
.dumps(GenerateResultsDict(test_run_result
)))
107 def ParseResultsFromJson(json_results
):
108 """Creates a list of BaseTestResult objects from JSON.
111 json_results: A JSON dict in the format created by
112 GenerateJsonResultsFile.
115 def string_as_status(s
):
117 return base_test_result
.ResultType
.PASS
119 return base_test_result
.ResultType
.SKIP
121 return base_test_result
.ResultType
.FAIL
123 return base_test_result
.ResultType
.CRASH
125 return base_test_result
.ResultType
.TIMEOUT
127 return base_test_result
.ResultType
.UNKNOWN
130 testsuite_runs
= json_results
['per_iteration_data']
131 for testsuite_run
in testsuite_runs
:
132 for test
, test_runs
in testsuite_run
.iteritems():
134 [base_test_result
.BaseTestResult(test
,
135 string_as_status(tr
['status']),
136 duration
=tr
['elapsed_time_ms'])
137 for tr
in test_runs
])