1 # Copyright (C) 2010 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
13 # * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 """Unit tests for json_results_generator.py."""
35 from webkitpy.common.system import filesystem_mock
36 from webkitpy.layout_tests.layout_package import json_results_generator
37 from webkitpy.layout_tests.models import test_expectations
38 from webkitpy.layout_tests.port import test
39 from webkitpy.thirdparty.mock import Mock
40 from webkitpy.thirdparty import simplejson
42 class JSONGeneratorTest(unittest.TestCase):
44 self.builder_name = 'DUMMY_BUILDER_NAME'
45 self.build_name = 'DUMMY_BUILD_NAME'
46 self.build_number = 'DUMMY_BUILDER_NUMBER'
48 # For archived results.
51 self._tests_set = set([])
52 self._test_timings = {}
53 self._failed_count_map = {}
56 self._DISABLED_count = 0
59 self._fixable_count = 0
61 def _test_json_generation(self, passed_tests_list, failed_tests_list):
62 tests_set = set(passed_tests_list) | set(failed_tests_list)
64 DISABLED_tests = set([t for t in tests_set
65 if t.startswith('DISABLED_')])
66 FLAKY_tests = set([t for t in tests_set
67 if t.startswith('FLAKY_')])
68 FAILS_tests = set([t for t in tests_set
69 if t.startswith('FAILS_')])
70 PASS_tests = tests_set - (DISABLED_tests | FLAKY_tests | FAILS_tests)
72 failed_tests = set(failed_tests_list) - DISABLED_tests
73 failed_count_map = dict([(t, 1) for t in failed_tests])
77 for test in tests_set:
78 test_timings[test] = float(self._num_runs * 100 + i)
81 test_results_map = dict()
82 for test in tests_set:
83 test_results_map[test] = json_results_generator.TestResult(test,
84 failed=(test in failed_tests),
85 elapsed_time=test_timings[test])
88 port._filesystem = filesystem_mock.MockFileSystem()
89 generator = json_results_generator.JSONResultsGeneratorBase(port,
90 self.builder_name, self.build_name, self.build_number,
92 None, # don't fetch past json results archive
95 failed_count_map = dict([(t, 1) for t in failed_tests])
97 # Test incremental json results
98 incremental_json = generator.get_json()
99 self._verify_json_results(
106 len(DISABLED_tests | failed_tests),
110 # We don't verify the results here, but at least we make sure the code runs without errors.
111 generator.generate_json_output()
112 generator.generate_times_ms_file()
114 def _verify_json_results(self, tests_set, test_timings, failed_count_map,
115 PASS_count, DISABLED_count, FLAKY_count,
118 # Aliasing to a short name for better access to its constants.
119 JRG = json_results_generator.JSONResultsGeneratorBase
121 self.assertTrue(JRG.VERSION_KEY in json)
122 self.assertTrue(self.builder_name in json)
124 buildinfo = json[self.builder_name]
125 self.assertTrue(JRG.FIXABLE in buildinfo)
126 self.assertTrue(JRG.TESTS in buildinfo)
127 self.assertEqual(len(buildinfo[JRG.BUILD_NUMBERS]), num_runs)
128 self.assertEqual(buildinfo[JRG.BUILD_NUMBERS][0], self.build_number)
130 if tests_set or DISABLED_count:
132 for fixable_items in buildinfo[JRG.FIXABLE]:
133 for (type, count) in fixable_items.iteritems():
135 fixable[type] = fixable[type] + count
137 fixable[type] = count
140 self.assertEqual(fixable[JRG.PASS_RESULT], PASS_count)
142 self.assertTrue(JRG.PASS_RESULT not in fixable or
143 fixable[JRG.PASS_RESULT] == 0)
145 self.assertEqual(fixable[JRG.SKIP_RESULT], DISABLED_count)
147 self.assertTrue(JRG.SKIP_RESULT not in fixable or
148 fixable[JRG.SKIP_RESULT] == 0)
150 self.assertEqual(fixable[JRG.FLAKY_RESULT], FLAKY_count)
152 self.assertTrue(JRG.FLAKY_RESULT not in fixable or
153 fixable[JRG.FLAKY_RESULT] == 0)
156 tests = buildinfo[JRG.TESTS]
157 for test_name in failed_count_map.iterkeys():
158 test = self._find_test_in_trie(test_name, tests)
161 for result in test[JRG.RESULTS]:
162 if result[1] == JRG.FAIL_RESULT:
164 self.assertEqual(failed_count_map[test_name], failed)
167 for timings in test[JRG.TIMES]:
168 if timings[1] == test_timings[test_name]:
169 timing_count = timings[0]
170 self.assertEqual(1, timing_count)
173 self.assertEqual(sum(buildinfo[JRG.FIXABLE_COUNT]), fixable_count)
175 def _find_test_in_trie(self, path, trie):
176 nodes = path.split("/")
179 self.assertTrue(node in sub_trie)
180 sub_trie = sub_trie[node]
183 def test_json_generation(self):
184 self._test_json_generation([], [])
185 self._test_json_generation(['A1', 'B1'], [])
186 self._test_json_generation([], ['FAILS_A2', 'FAILS_B2'])
187 self._test_json_generation(['DISABLED_A3', 'DISABLED_B3'], [])
188 self._test_json_generation(['A4'], ['B4', 'FAILS_C4'])
189 self._test_json_generation(['DISABLED_C5', 'DISABLED_D5'], ['A5', 'B5'])
190 self._test_json_generation(
191 ['A6', 'B6', 'FAILS_C6', 'DISABLED_E6', 'DISABLED_F6'],
194 # Generate JSON with the same test sets. (Both incremental results and
195 # archived results must be updated appropriately.)
196 self._test_json_generation(
197 ['A', 'FLAKY_B', 'DISABLED_C'],
198 ['FAILS_D', 'FLAKY_E'])
199 self._test_json_generation(
200 ['A', 'DISABLED_C', 'FLAKY_E'],
201 ['FLAKY_B', 'FAILS_D'])
202 self._test_json_generation(
203 ['FLAKY_B', 'DISABLED_C', 'FAILS_D'],
206 def test_hierarchical_json_generation(self):
207 # FIXME: Re-work tests to be more comprehensible and comprehensive.
208 self._test_json_generation(['foo/A'], ['foo/B', 'bar/C'])
210 def test_test_timings_trie(self):
211 test_port = test.TestPort()
212 individual_test_timings = []
213 individual_test_timings.append(json_results_generator.TestResult('foo/bar/baz.html', elapsed_time=1.2))
214 individual_test_timings.append(json_results_generator.TestResult('bar.html', elapsed_time=0.0001))
215 trie = json_results_generator.test_timings_trie(test_port, individual_test_timings)
226 self.assertEqual(simplejson.dumps(trie), simplejson.dumps(expected_trie))
230 if __name__ == '__main__':