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