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 7 from pylib.base import base_test_result 8 9 10 def GenerateResultsDict(test_run_results): 11 """Create a results dict from |test_run_results| suitable for writing to JSON. 12 Args: 13 test_run_results: a list of base_test_result.TestRunResults objects. 14 Returns: 15 A results dict that mirrors the one generated by 16 base/test/launcher/test_results_tracker.cc:SaveSummaryAsJSON. 17 """ 18 # Example json output. 19 # { 20 # "global_tags": [], 21 # "all_tests": [ 22 # "test1", 23 # "test2", 24 # ], 25 # "disabled_tests": [], 26 # "per_iteration_data": [ 27 # { 28 # "test1": [ 29 # { 30 # "status": "SUCCESS", 31 # "elapsed_time_ms": 1, 32 # "output_snippet": "", 33 # "output_snippet_base64": "", 34 # "losless_snippet": "", 35 # }, 36 # ], 37 # "test2": [ 38 # { 39 # "status": "FAILURE", 40 # "elapsed_time_ms": 12, 41 # "output_snippet": "", 42 # "output_snippet_base64": "", 43 # "losless_snippet": "", 44 # }, 45 # ], 46 # }, 47 # { 48 # "test1": [ 49 # { 50 # "status": "SUCCESS", 51 # "elapsed_time_ms": 1, 52 # "output_snippet": "", 53 # "output_snippet_base64": "", 54 # "losless_snippet": "", 55 # }, 56 # ], 57 # "test2": [ 58 # { 59 # "status": "FAILURE", 60 # "elapsed_time_ms": 12, 61 # "output_snippet": "", 62 # "output_snippet_base64": "", 63 # "losless_snippet": "", 64 # }, 65 # ], 66 # }, 67 # ... 68 # ], 69 # } 70 71 def status_as_string(s): 72 if s == base_test_result.ResultType.PASS: 73 return 'SUCCESS' 74 elif s == base_test_result.ResultType.SKIP: 75 return 'SKIPPED' 76 elif s == base_test_result.ResultType.FAIL: 77 return 'FAILURE' 78 elif s == base_test_result.ResultType.CRASH: 79 return 'CRASH' 80 elif s == base_test_result.ResultType.TIMEOUT: 81 return 'TIMEOUT' 82 elif s == base_test_result.ResultType.UNKNOWN: 83 return 'UNKNOWN' 84 85 all_tests = set() 86 per_iteration_data = [] 87 for test_run_result in test_run_results: 88 iteration_data = { 89 t.GetName(): [{ 90 'status': status_as_string(t.GetType()), 91 'elapsed_time_ms': t.GetDuration(), 92 'output_snippet': '', 93 'losless_snippet': '', 94 'output_snippet_base64:': '', 95 }] 96 for t in test_run_result.GetAll() 97 } 98 all_tests = all_tests.union(set(iteration_data.iterkeys())) 99 per_iteration_data.append(iteration_data) 100 101 return { 102 'global_tags': [], 103 'all_tests': sorted(list(all_tests)), 104 # TODO(jbudorick): Add support for disabled tests within base_test_result. 105 'disabled_tests': [], 106 'per_iteration_data': per_iteration_data, 107 } 108 109 110 def GenerateJsonResultsFile(test_run_result, file_path): 111 """Write |test_run_result| to JSON. 112 113 This emulates the format of the JSON emitted by 114 base/test/launcher/test_results_tracker.cc:SaveSummaryAsJSON. 115 116 Args: 117 test_run_result: a base_test_result.TestRunResults object. 118 file_path: The path to the JSON file to write. 119 """ 120 with open(file_path, 'w') as json_result_file: 121 json_result_file.write(json.dumps(GenerateResultsDict(test_run_result))) 122 123 124 def ParseResultsFromJson(json_results): 125 """Creates a list of BaseTestResult objects from JSON. 126 127 Args: 128 json_results: A JSON dict in the format created by 129 GenerateJsonResultsFile. 130 """ 131 132 def string_as_status(s): 133 if s == 'SUCCESS': 134 return base_test_result.ResultType.PASS 135 elif s == 'SKIPPED': 136 return base_test_result.ResultType.SKIP 137 elif s == 'FAILURE': 138 return base_test_result.ResultType.FAIL 139 elif s == 'CRASH': 140 return base_test_result.ResultType.CRASH 141 elif s == 'TIMEOUT': 142 return base_test_result.ResultType.TIMEOUT 143 else: 144 return base_test_result.ResultType.UNKNOWN 145 146 results_list = [] 147 testsuite_runs = json_results['per_iteration_data'] 148 for testsuite_run in testsuite_runs: 149 for test, test_runs in testsuite_run.iteritems(): 150 results_list.extend( 151 [base_test_result.BaseTestResult(test, 152 string_as_status(tr['status']), 153 duration=tr['elapsed_time_ms']) 154 for tr in test_runs]) 155 return results_list 156 157