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.
6 # Most of this file was ported over from Blink's
7 # webkitpy/layout_tests/layout_package/json_results_generator_unittest.py
13 from pylib
.utils
import json_results_generator
16 class JSONGeneratorTest(unittest
.TestCase
):
19 self
.builder_name
= 'DUMMY_BUILDER_NAME'
20 self
.build_name
= 'DUMMY_BUILD_NAME'
21 self
.build_number
= 'DUMMY_BUILDER_NUMBER'
23 # For archived results.
26 self
._tests
_set
= set([])
27 self
._test
_timings
= {}
28 self
._failed
_count
_map
= {}
31 self
._DISABLED
_count
= 0
34 self
._fixable
_count
= 0
36 self
._orig
_write
_json
= json_results_generator
.WriteJSON
38 # unused arguments ... pylint: disable=W0613
39 def _WriteJSONStub(json_object
, file_path
, callback
=None):
42 json_results_generator
.WriteJSON
= _WriteJSONStub
45 json_results_generator
.WriteJSON
= self
._orig
_write
_json
47 def _TestJSONGeneration(self
, passed_tests_list
, failed_tests_list
):
48 tests_set
= set(passed_tests_list
) |
set(failed_tests_list
)
50 DISABLED_tests
= set([t
for t
in tests_set
51 if t
.startswith('DISABLED_')])
52 FLAKY_tests
= set([t
for t
in tests_set
53 if t
.startswith('FLAKY_')])
54 FAILS_tests
= set([t
for t
in tests_set
55 if t
.startswith('FAILS_')])
56 PASS_tests
= tests_set
- (DISABLED_tests | FLAKY_tests | FAILS_tests
)
58 failed_tests
= set(failed_tests_list
) - DISABLED_tests
59 failed_count_map
= dict([(t
, 1) for t
in failed_tests
])
63 for test
in tests_set
:
64 test_timings
[test
] = float(self
._num
_runs
* 100 + i
)
67 test_results_map
= dict()
68 for test
in tests_set
:
69 test_results_map
[test
] = json_results_generator
.TestResult(
70 test
, failed
=(test
in failed_tests
),
71 elapsed_time
=test_timings
[test
])
73 generator
= json_results_generator
.JSONResultsGeneratorBase(
74 self
.builder_name
, self
.build_name
, self
.build_number
,
76 None, # don't fetch past json results archive
79 failed_count_map
= dict([(t
, 1) for t
in failed_tests
])
81 # Test incremental json results
82 incremental_json
= generator
.GetJSON()
83 self
._VerifyJSONResults
(
90 len(DISABLED_tests | failed_tests
),
94 # We don't verify the results here, but at least we make sure the code
95 # runs without errors.
96 generator
.GenerateJSONOutput()
97 generator
.GenerateTimesMSFile()
99 def _VerifyJSONResults(self
, tests_set
, test_timings
, failed_count_map
,
100 PASS_count
, DISABLED_count
, FLAKY_count
,
101 fixable_count
, json_obj
, num_runs
):
102 # Aliasing to a short name for better access to its constants.
103 JRG
= json_results_generator
.JSONResultsGeneratorBase
105 self
.assertIn(JRG
.VERSION_KEY
, json_obj
)
106 self
.assertIn(self
.builder_name
, json_obj
)
108 buildinfo
= json_obj
[self
.builder_name
]
109 self
.assertIn(JRG
.FIXABLE
, buildinfo
)
110 self
.assertIn(JRG
.TESTS
, buildinfo
)
111 self
.assertEqual(len(buildinfo
[JRG
.BUILD_NUMBERS
]), num_runs
)
112 self
.assertEqual(buildinfo
[JRG
.BUILD_NUMBERS
][0], self
.build_number
)
114 if tests_set
or DISABLED_count
:
116 for fixable_items
in buildinfo
[JRG
.FIXABLE
]:
117 for (result_type
, count
) in fixable_items
.iteritems():
118 if result_type
in fixable
:
119 fixable
[result_type
] = fixable
[result_type
] + count
121 fixable
[result_type
] = count
124 self
.assertEqual(fixable
[JRG
.PASS_RESULT
], PASS_count
)
126 self
.assertTrue(JRG
.PASS_RESULT
not in fixable
or
127 fixable
[JRG
.PASS_RESULT
] == 0)
129 self
.assertEqual(fixable
[JRG
.SKIP_RESULT
], DISABLED_count
)
131 self
.assertTrue(JRG
.SKIP_RESULT
not in fixable
or
132 fixable
[JRG
.SKIP_RESULT
] == 0)
134 self
.assertEqual(fixable
[JRG
.FLAKY_RESULT
], FLAKY_count
)
136 self
.assertTrue(JRG
.FLAKY_RESULT
not in fixable
or
137 fixable
[JRG
.FLAKY_RESULT
] == 0)
140 tests
= buildinfo
[JRG
.TESTS
]
141 for test_name
in failed_count_map
.iterkeys():
142 test
= self
._FindTestInTrie
(test_name
, tests
)
145 for result
in test
[JRG
.RESULTS
]:
146 if result
[1] == JRG
.FAIL_RESULT
:
148 self
.assertEqual(failed_count_map
[test_name
], failed
)
151 for timings
in test
[JRG
.TIMES
]:
152 if timings
[1] == test_timings
[test_name
]:
153 timing_count
= timings
[0]
154 self
.assertEqual(1, timing_count
)
157 self
.assertEqual(sum(buildinfo
[JRG
.FIXABLE_COUNT
]), fixable_count
)
159 def _FindTestInTrie(self
, path
, trie
):
160 nodes
= path
.split('/')
163 self
.assertIn(node
, sub_trie
)
164 sub_trie
= sub_trie
[node
]
167 def testJSONGeneration(self
):
168 self
._TestJSONGeneration
([], [])
169 self
._TestJSONGeneration
(['A1', 'B1'], [])
170 self
._TestJSONGeneration
([], ['FAILS_A2', 'FAILS_B2'])
171 self
._TestJSONGeneration
(['DISABLED_A3', 'DISABLED_B3'], [])
172 self
._TestJSONGeneration
(['A4'], ['B4', 'FAILS_C4'])
173 self
._TestJSONGeneration
(['DISABLED_C5', 'DISABLED_D5'], ['A5', 'B5'])
174 self
._TestJSONGeneration
(
175 ['A6', 'B6', 'FAILS_C6', 'DISABLED_E6', 'DISABLED_F6'],
178 # Generate JSON with the same test sets. (Both incremental results and
179 # archived results must be updated appropriately.)
180 self
._TestJSONGeneration
(
181 ['A', 'FLAKY_B', 'DISABLED_C'],
182 ['FAILS_D', 'FLAKY_E'])
183 self
._TestJSONGeneration
(
184 ['A', 'DISABLED_C', 'FLAKY_E'],
185 ['FLAKY_B', 'FAILS_D'])
186 self
._TestJSONGeneration
(
187 ['FLAKY_B', 'DISABLED_C', 'FAILS_D'],
190 def testHierarchicalJSNGeneration(self
):
191 # FIXME: Re-work tests to be more comprehensible and comprehensive.
192 self
._TestJSONGeneration
(['foo/A'], ['foo/B', 'bar/C'])
194 def testTestTimingsTrie(self
):
195 individual_test_timings
= []
196 individual_test_timings
.append(
197 json_results_generator
.TestResult(
200 individual_test_timings
.append(
201 json_results_generator
.TestResult('bar.html', elapsed_time
=0.0001))
202 trie
= json_results_generator
.TestTimingsTrie(individual_test_timings
)
213 self
.assertEqual(json
.dumps(trie
), json
.dumps(expected_trie
))