forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standard_json_util.py
146 lines (112 loc) · 4.25 KB
/
standard_json_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import logging
from collections import OrderedDict
import os
import result_sink_util
LOGGER = logging.getLogger(__name__)
class StdJson():
def __init__(self, **kwargs):
"""Module for storing the results in standard JSON format.
https://chromium.googlesource.com/chromium/src/+/main/docs/testing/json_test_results_format.md
"""
self.tests = OrderedDict()
self.result_sink = result_sink_util.ResultSinkClient()
self._shard_index = os.getenv('GTEST_SHARD_INDEX', 0)
if 'passed' in kwargs:
self.mark_all_passed(kwargs['passed'])
if 'failed' in kwargs:
self.mark_all_failed(kwargs['failed'])
if 'flaked' in kwargs:
self.mark_all_passed(kwargs['flaked'], flaky=True)
def _init_test(self, expected, actual, is_unexpected=False):
"""Returns a dict of test result info used as values in self.tests dict."""
test = {
'expected': expected,
'actual': actual,
'shard': self._shard_index,
}
if is_unexpected:
test['is_unexpected'] = True
return test
def finalize(self):
"""Teardown and finalizing tasks needed after all results are reported."""
LOGGER.info('Finalizing in standard json util.')
self.result_sink.close()
def mark_passed(self, test, flaky=False):
"""Sets test as passed
Params:
test (str): a test in format "{TestCase}/{testMethod}"
If flaky=True, or if 'FAIL' already set in 'actual',
apply is_flaky=True for all test(s).
"""
if not test:
LOGGER.warn('Empty or None test name passed to standard_json_util')
return
self.result_sink.post(test, 'PASS', True)
if test in self.tests:
self.tests[test]['actual'] = self.tests[test]['actual'] + " PASS"
else:
self.tests[test] = self._init_test('PASS', 'PASS')
if flaky or 'FAIL' in self.tests[test]['actual']:
self.tests[test]['is_flaky'] = True
self.tests[test].pop('is_unexpected', None)
def mark_all_passed(self, tests, flaky=False):
"""Marks all tests as PASS"""
for test in tests:
self.mark_passed(test, flaky)
def mark_failed(self, test, test_log=None):
"""Sets test(s) as failed.
Params:
test (str): a test in format "{TestCase}/{testMethod}"
test_log (str): log of the specific test
"""
if not test:
LOGGER.warn('Empty or None test name passed to standard_json_util')
return
self.result_sink.post(test, 'FAIL', False, test_log=test_log)
if test in self.tests:
self.tests[test]['actual'] = self.tests[test]['actual'] + " FAIL"
self.tests[test]['is_unexpected'] = True
else:
self.tests[test] = self._init_test('PASS', 'FAIL', True)
def mark_all_failed(self, tests):
"""Marks all tests as FAIL"""
for test in tests:
self.mark_failed(test)
def mark_disabled(self, test):
"""Sets test(s) as expected SKIP with disabled test label.
Params:
test (str): a test in format "{TestCase}/{testMethod}"
"""
if not test:
LOGGER.warn('Empty or None test name passed to standard_json_util')
return
self.result_sink.post(test, 'SKIP', True, tags=[('disabled_test', 'true')])
self.tests[test] = self._init_test('SKIP', 'SKIP')
def mark_all_disabled(self, tests):
for test in tests:
self.mark_disabled(test)
def mark_timeout(self, test):
"""Sets test as TIMEOUT, which is used to indicate a test abort/timeout
Params:
test (str): a test in format "{TestCase}/{testMethod}"
"""
if not test:
LOGGER.warn('Empty or None test name passed to standard_json_util')
return
# Timeout tests in iOS test runner are tests that's unexpectedly not run.
test_log = ('The test is compiled in test target but was unexpectedly not'
' run or not finished.')
self.result_sink.post(
test,
'SKIP',
False,
test_log=test_log,
tags=[('disabled_test', 'false')])
if test in self.tests:
self.tests[test]['actual'] = self.tests[test]['actual'] + " TIMEOUT"
self.tests[test]['is_unexpected'] = True
else:
self.tests[test] = self._init_test('PASS', 'TIMEOUT', True)