Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(672)

Side by Side Diff: tools/telemetry/telemetry/user_story/user_story_runner_unittest.py

Issue 725133005: Move serving_dirs to UserStorySet, update user_story_runner. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import unittest 5 import unittest
6 import StringIO 6 import StringIO
7 import sys 7 import sys
8 8
9 from telemetry import benchmark 9 from telemetry import benchmark
10 from telemetry import user_story 10 from telemetry import user_story
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 class DummyTest(page_test.PageTest): 78 class DummyTest(page_test.PageTest):
79 def RunPage(self, *_): 79 def RunPage(self, *_):
80 pass 80 pass
81 81
82 82
83 class EmptyMetadataForTest(benchmark.BenchmarkMetadata): 83 class EmptyMetadataForTest(benchmark.BenchmarkMetadata):
84 def __init__(self): 84 def __init__(self):
85 super(EmptyMetadataForTest, self).__init__('') 85 super(EmptyMetadataForTest, self).__init__('')
86 86
87 87
88 class DummyLocalUserStory(user_story.UserStory):
89 def __init__(self, shared_user_story_state_class, name=''):
90 super(DummyLocalUserStory, self).__init__(
91 shared_user_story_state_class, name=name)
92
93 @property
94 def is_local(self):
95 return True
96
88 def _GetOptionForUnittest(): 97 def _GetOptionForUnittest():
89 options = options_for_unittests.GetCopy() 98 options = options_for_unittests.GetCopy()
90 options.output_formats = ['none'] 99 options.output_formats = ['none']
91 options.suppress_gtest_report = True 100 options.suppress_gtest_report = True
92 parser = options.CreateParser() 101 parser = options.CreateParser()
93 user_story_runner.AddCommandLineArgs(parser) 102 user_story_runner.AddCommandLineArgs(parser)
94 options.MergeDefaultValues(parser.get_default_values()) 103 options.MergeDefaultValues(parser.get_default_values())
95 user_story_runner.ProcessCommandLineArgs(parser, options) 104 user_story_runner.ProcessCommandLineArgs(parser, options)
96 return options 105 return options
97 106
(...skipping 28 matching lines...) Expand all
126 user_story_runner.exception_formatter = exception_formatter_module 135 user_story_runner.exception_formatter = exception_formatter_module
127 if self._user_story_runner_logging_stub: 136 if self._user_story_runner_logging_stub:
128 self._user_story_runner_logging_stub.Restore() 137 self._user_story_runner_logging_stub.Restore()
129 self._user_story_runner_logging_stub = None 138 self._user_story_runner_logging_stub = None
130 139
131 def tearDown(self): 140 def tearDown(self):
132 self.RestoreExceptionFormatter() 141 self.RestoreExceptionFormatter()
133 142
134 def testGetUserStoryGroupsWithSameSharedUserStoryClass(self): 143 def testGetUserStoryGroupsWithSameSharedUserStoryClass(self):
135 us = user_story_set.UserStorySet() 144 us = user_story_set.UserStorySet()
136 us.AddUserStory(user_story.UserStory(FooUserStoryState)) 145 us.AddUserStory(DummyLocalUserStory(FooUserStoryState))
137 us.AddUserStory(user_story.UserStory(FooUserStoryState)) 146 us.AddUserStory(DummyLocalUserStory(FooUserStoryState))
138 us.AddUserStory(user_story.UserStory(BarUserStoryState)) 147 us.AddUserStory(DummyLocalUserStory(BarUserStoryState))
139 us.AddUserStory(user_story.UserStory(FooUserStoryState)) 148 us.AddUserStory(DummyLocalUserStory(FooUserStoryState))
140 story_groups = ( 149 story_groups = (
141 user_story_runner.GetUserStoryGroupsWithSameSharedUserStoryClass( 150 user_story_runner.GetUserStoryGroupsWithSameSharedUserStoryClass(
142 us)) 151 us))
143 self.assertEqual(len(story_groups), 3) 152 self.assertEqual(len(story_groups), 3)
144 self.assertEqual(story_groups[0].shared_user_story_state_class, 153 self.assertEqual(story_groups[0].shared_user_story_state_class,
145 FooUserStoryState) 154 FooUserStoryState)
146 self.assertEqual(story_groups[1].shared_user_story_state_class, 155 self.assertEqual(story_groups[1].shared_user_story_state_class,
147 BarUserStoryState) 156 BarUserStoryState)
148 self.assertEqual(story_groups[2].shared_user_story_state_class, 157 self.assertEqual(story_groups[2].shared_user_story_state_class,
149 FooUserStoryState) 158 FooUserStoryState)
150 159
151 def testSuccefulUserStoryTest(self): 160 def testSuccefulUserStoryTest(self):
152 us = user_story_set.UserStorySet() 161 us = user_story_set.UserStorySet()
153 us.AddUserStory(user_story.UserStory(FooUserStoryState)) 162 us.AddUserStory(DummyLocalUserStory(FooUserStoryState))
154 us.AddUserStory(user_story.UserStory(FooUserStoryState)) 163 us.AddUserStory(DummyLocalUserStory(FooUserStoryState))
155 us.AddUserStory(user_story.UserStory(BarUserStoryState)) 164 us.AddUserStory(DummyLocalUserStory(BarUserStoryState))
156 user_story_runner.Run( 165 user_story_runner.Run(
157 DummyTest(), us, self.expectations, self.options, self.results) 166 DummyTest(), us, self.expectations, self.options, self.results)
158 self.assertEquals(0, len(self.results.failures)) 167 self.assertEquals(0, len(self.results.failures))
159 self.assertEquals(3, GetNumberOfSuccessfulPageRuns(self.results)) 168 self.assertEquals(3, GetNumberOfSuccessfulPageRuns(self.results))
160 169
161 def testTearDownIsCalledOnceForEachUserStoryGroupWithPageSetRepeat(self): 170 def testTearDownIsCalledOnceForEachUserStoryGroupWithPageSetRepeat(self):
162 self.options.pageset_repeat = 3 171 self.options.pageset_repeat = 3
163 us = user_story_set.UserStorySet() 172 us = user_story_set.UserStorySet()
164 fooz_init_call_counter = [0] 173 fooz_init_call_counter = [0]
165 fooz_tear_down_call_counter = [0] 174 fooz_tear_down_call_counter = [0]
166 barz_init_call_counter = [0] 175 barz_init_call_counter = [0]
167 barz_tear_down_call_counter = [0] 176 barz_tear_down_call_counter = [0]
168 class FoozUserStoryState(FooUserStoryState): 177 class FoozUserStoryState(FooUserStoryState):
169 def __init__(self, test, options, user_story_setz): 178 def __init__(self, test, options, user_story_setz):
170 super(FoozUserStoryState, self).__init__( 179 super(FoozUserStoryState, self).__init__(
171 test, options, user_story_setz) 180 test, options, user_story_setz)
172 fooz_init_call_counter[0] += 1 181 fooz_init_call_counter[0] += 1
173 def TearDownState(self, _results): 182 def TearDownState(self, _results):
174 fooz_tear_down_call_counter[0] += 1 183 fooz_tear_down_call_counter[0] += 1
175 184
176 class BarzUserStoryState(BarUserStoryState): 185 class BarzUserStoryState(BarUserStoryState):
177 def __init__(self, test, options, user_story_setz): 186 def __init__(self, test, options, user_story_setz):
178 super(BarzUserStoryState, self).__init__( 187 super(BarzUserStoryState, self).__init__(
179 test, options, user_story_setz) 188 test, options, user_story_setz)
180 barz_init_call_counter[0] += 1 189 barz_init_call_counter[0] += 1
181 def TearDownState(self, _results): 190 def TearDownState(self, _results):
182 barz_tear_down_call_counter[0] += 1 191 barz_tear_down_call_counter[0] += 1
183 192
184 us.AddUserStory(user_story.UserStory(FoozUserStoryState)) 193 us.AddUserStory(DummyLocalUserStory(FoozUserStoryState))
185 us.AddUserStory(user_story.UserStory(FoozUserStoryState)) 194 us.AddUserStory(DummyLocalUserStory(FoozUserStoryState))
186 us.AddUserStory(user_story.UserStory(BarzUserStoryState)) 195 us.AddUserStory(DummyLocalUserStory(BarzUserStoryState))
187 us.AddUserStory(user_story.UserStory(BarzUserStoryState)) 196 us.AddUserStory(DummyLocalUserStory(BarzUserStoryState))
188 user_story_runner.Run( 197 user_story_runner.Run(
189 DummyTest(), us, self.expectations, self.options, self.results) 198 DummyTest(), us, self.expectations, self.options, self.results)
190 self.assertEquals(0, len(self.results.failures)) 199 self.assertEquals(0, len(self.results.failures))
191 self.assertEquals(12, GetNumberOfSuccessfulPageRuns(self.results)) 200 self.assertEquals(12, GetNumberOfSuccessfulPageRuns(self.results))
192 self.assertEquals(1, fooz_init_call_counter[0]) 201 self.assertEquals(1, fooz_init_call_counter[0])
193 self.assertEquals(1, fooz_tear_down_call_counter[0]) 202 self.assertEquals(1, fooz_tear_down_call_counter[0])
194 self.assertEquals(1, barz_init_call_counter[0]) 203 self.assertEquals(1, barz_init_call_counter[0])
195 self.assertEquals(1, barz_tear_down_call_counter[0]) 204 self.assertEquals(1, barz_tear_down_call_counter[0])
196 205
197 def testHandlingOfCrashedApp(self): 206 def testHandlingOfCrashedApp(self):
198 self.SuppressExceptionFormatting() 207 self.SuppressExceptionFormatting()
199 us = user_story_set.UserStorySet() 208 us = user_story_set.UserStorySet()
200 class SharedUserStoryThatCausesAppCrash(TestSharedUserStoryState): 209 class SharedUserStoryThatCausesAppCrash(TestSharedUserStoryState):
201 def WillRunUserStory(self, user_storyz): 210 def WillRunUserStory(self, user_storyz):
202 raise exceptions.AppCrashException() 211 raise exceptions.AppCrashException()
203 212
204 us.AddUserStory(user_story.UserStory(SharedUserStoryThatCausesAppCrash)) 213 us.AddUserStory(DummyLocalUserStory(SharedUserStoryThatCausesAppCrash))
205 user_story_runner.Run( 214 user_story_runner.Run(
206 DummyTest(), us, self.expectations, self.options, self.results) 215 DummyTest(), us, self.expectations, self.options, self.results)
207 self.assertEquals(1, len(self.results.failures)) 216 self.assertEquals(1, len(self.results.failures))
208 self.assertEquals(0, GetNumberOfSuccessfulPageRuns(self.results)) 217 self.assertEquals(0, GetNumberOfSuccessfulPageRuns(self.results))
209 218
210 def testHandlingOfTestThatRaisesWithNonFatalUnknownExceptions(self): 219 def testHandlingOfTestThatRaisesWithNonFatalUnknownExceptions(self):
211 self.SuppressExceptionFormatting() 220 self.SuppressExceptionFormatting()
212 us = user_story_set.UserStorySet() 221 us = user_story_set.UserStorySet()
213 222
214 class ExpectedException(Exception): 223 class ExpectedException(Exception):
215 pass 224 pass
216 225
217 class Test(page_test.PageTest): 226 class Test(page_test.PageTest):
218 def __init__(self, *args): 227 def __init__(self, *args):
219 super(Test, self).__init__(*args) 228 super(Test, self).__init__(*args)
220 self.run_count = 0 229 self.run_count = 0
221 def RunPage(self, *_): 230 def RunPage(self, *_):
222 old_run_count = self.run_count 231 old_run_count = self.run_count
223 self.run_count += 1 232 self.run_count += 1
224 if old_run_count == 0: 233 if old_run_count == 0:
225 raise ExpectedException() 234 raise ExpectedException()
226 235
227 us.AddUserStory(user_story.UserStory(TestSharedUserStoryState)) 236 us.AddUserStory(DummyLocalUserStory(TestSharedUserStoryState))
228 us.AddUserStory(user_story.UserStory(TestSharedUserStoryState)) 237 us.AddUserStory(DummyLocalUserStory(TestSharedUserStoryState))
229 test = Test() 238 test = Test()
230 user_story_runner.Run( 239 user_story_runner.Run(
231 test, us, self.expectations, self.options, self.results) 240 test, us, self.expectations, self.options, self.results)
232 self.assertEquals(2, test.run_count) 241 self.assertEquals(2, test.run_count)
233 self.assertEquals(1, len(self.results.failures)) 242 self.assertEquals(1, len(self.results.failures))
234 self.assertEquals(1, GetNumberOfSuccessfulPageRuns(self.results)) 243 self.assertEquals(1, GetNumberOfSuccessfulPageRuns(self.results))
235 244
236 def testRaiseBrowserGoneExceptionFromRunPage(self): 245 def testRaiseBrowserGoneExceptionFromRunPage(self):
237 self.SuppressExceptionFormatting() 246 self.SuppressExceptionFormatting()
238 us = user_story_set.UserStorySet() 247 us = user_story_set.UserStorySet()
239 248
240 class Test(page_test.PageTest): 249 class Test(page_test.PageTest):
241 def __init__(self, *args): 250 def __init__(self, *args):
242 super(Test, self).__init__(*args) 251 super(Test, self).__init__(*args)
243 self.run_count = 0 252 self.run_count = 0
244 def RunPage(self, *_): 253 def RunPage(self, *_):
245 old_run_count = self.run_count 254 old_run_count = self.run_count
246 self.run_count += 1 255 self.run_count += 1
247 if old_run_count == 0: 256 if old_run_count == 0:
248 raise exceptions.BrowserGoneException() 257 raise exceptions.BrowserGoneException()
249 258
250 us.AddUserStory(user_story.UserStory(TestSharedUserStoryState)) 259 us.AddUserStory(DummyLocalUserStory(TestSharedUserStoryState))
251 us.AddUserStory(user_story.UserStory(TestSharedUserStoryState)) 260 us.AddUserStory(DummyLocalUserStory(TestSharedUserStoryState))
252 test = Test() 261 test = Test()
253 user_story_runner.Run( 262 user_story_runner.Run(
254 test, us, self.expectations, self.options, self.results) 263 test, us, self.expectations, self.options, self.results)
255 self.assertEquals(2, test.run_count) 264 self.assertEquals(2, test.run_count)
256 self.assertEquals(1, len(self.results.failures)) 265 self.assertEquals(1, len(self.results.failures))
257 self.assertEquals(1, GetNumberOfSuccessfulPageRuns(self.results)) 266 self.assertEquals(1, GetNumberOfSuccessfulPageRuns(self.results))
258 267
259 def testDiscardFirstResult(self): 268 def testDiscardFirstResult(self):
260 us = user_story_set.UserStorySet() 269 us = user_story_set.UserStorySet()
261 us.AddUserStory(user_story.UserStory(TestSharedUserStoryState)) 270 us.AddUserStory(DummyLocalUserStory(TestSharedUserStoryState))
262 us.AddUserStory(user_story.UserStory(TestSharedUserStoryState)) 271 us.AddUserStory(DummyLocalUserStory(TestSharedUserStoryState))
263 class Measurement(page_test.PageTest): 272 class Measurement(page_test.PageTest):
264 @property 273 @property
265 def discard_first_result(self): 274 def discard_first_result(self):
266 return True 275 return True
267 276
268 def RunPage(self, page, _, results): 277 def RunPage(self, page, _, results):
269 results.AddValue(string.StringValue(page, 'test', 't', page.name)) 278 results.AddValue(string.StringValue(page, 'test', 't', page.name))
270 279
271 results = results_options.CreateResults( 280 results = results_options.CreateResults(
272 EmptyMetadataForTest(), self.options) 281 EmptyMetadataForTest(), self.options)
(...skipping 30 matching lines...) Expand all
303 self.options.page_repeat = 1 312 self.options.page_repeat = 1
304 self.options.pageset_repeat = 1 313 self.options.pageset_repeat = 1
305 user_story_runner.Run( 314 user_story_runner.Run(
306 Measurement(), us, self.expectations, self.options, results) 315 Measurement(), us, self.expectations, self.options, results)
307 self.assertEquals(0, GetNumberOfSuccessfulPageRuns(results)) 316 self.assertEquals(0, GetNumberOfSuccessfulPageRuns(results))
308 self.assertEquals(0, len(results.failures)) 317 self.assertEquals(0, len(results.failures))
309 self.assertEquals(0, len(results.all_page_specific_values)) 318 self.assertEquals(0, len(results.all_page_specific_values))
310 319
311 def testPagesetRepeat(self): 320 def testPagesetRepeat(self):
312 us = user_story_set.UserStorySet() 321 us = user_story_set.UserStorySet()
313 us.AddUserStory(user_story.UserStory( 322 us.AddUserStory(DummyLocalUserStory(
314 TestSharedUserStoryState, name='blank')) 323 TestSharedUserStoryState, name='blank'))
315 us.AddUserStory(user_story.UserStory( 324 us.AddUserStory(DummyLocalUserStory(
316 TestSharedUserStoryState, name='green')) 325 TestSharedUserStoryState, name='green'))
317 326
318 class Measurement(page_test.PageTest): 327 class Measurement(page_test.PageTest):
319 i = 0 328 i = 0
320 def RunPage(self, page, _, results): 329 def RunPage(self, page, _, results):
321 self.i += 1 330 self.i += 1
322 results.AddValue(scalar.ScalarValue( 331 results.AddValue(scalar.ScalarValue(
323 page, 'metric', 'unit', self.i)) 332 page, 'metric', 'unit', self.i))
324 333
325 self.options.page_repeat = 1 334 self.options.page_repeat = 1
(...skipping 11 matching lines...) Expand all
337 contents = output.getvalue() 346 contents = output.getvalue()
338 self.assertEquals(4, GetNumberOfSuccessfulPageRuns(results)) 347 self.assertEquals(4, GetNumberOfSuccessfulPageRuns(results))
339 self.assertEquals(0, len(results.failures)) 348 self.assertEquals(0, len(results.failures))
340 self.assertIn('RESULT metric: blank= [1,3] unit', contents) 349 self.assertIn('RESULT metric: blank= [1,3] unit', contents)
341 self.assertIn('RESULT metric: green= [2,4] unit', contents) 350 self.assertIn('RESULT metric: green= [2,4] unit', contents)
342 self.assertIn('*RESULT metric: metric= [1,2,3,4] unit', contents) 351 self.assertIn('*RESULT metric: metric= [1,2,3,4] unit', contents)
343 finally: 352 finally:
344 sys.stdout = real_stdout 353 sys.stdout = real_stdout
345 354
346 def testCheckArchives(self): 355 def testCheckArchives(self):
347 ps = page_set.PageSet() 356 ps = page_set.PageSet()
nednguyen 2014/12/08 00:45:55 This test now can modified to test user_story_set
chrishenry 2014/12/08 19:02:40 Done.
348 ps.AddPageWithDefaultRunNavigate('http://www.testurl.com') 357 ps.AddPageWithDefaultRunNavigate('http://www.testurl.com')
349 # Page set missing archive_data_file. 358 # Page set missing archive_data_file.
350 self.assertFalse(user_story_runner._CheckArchives( 359 self.assertFalse(user_story_runner._CheckArchives(ps))
351 ps.archive_data_file, ps.wpr_archive_info, ps.pages))
352 360
353 ps = page_set.PageSet(archive_data_file='missing_archive_data_file.json') 361 ps = page_set.PageSet(archive_data_file='missing_archive_data_file.json')
354 ps.AddPageWithDefaultRunNavigate('http://www.testurl.com') 362 ps.AddPageWithDefaultRunNavigate('http://www.testurl.com')
355 # Page set missing json file specified in archive_data_file. 363 # Page set missing json file specified in archive_data_file.
356 self.assertFalse(user_story_runner._CheckArchives( 364 self.assertFalse(user_story_runner._CheckArchives(ps))
357 ps.archive_data_file, ps.wpr_archive_info, ps.pages))
358 365
359 ps = page_set.PageSet(archive_data_file='../../unittest_data/test.json', 366 ps = page_set.PageSet(archive_data_file='../../unittest_data/test.json',
360 bucket=page_set.PUBLIC_BUCKET) 367 bucket=page_set.PUBLIC_BUCKET)
361 ps.AddPageWithDefaultRunNavigate('http://www.testurl.com') 368 ps.AddPageWithDefaultRunNavigate('http://www.testurl.com')
362 # Page set with valid archive_data_file. 369 # Page set with valid archive_data_file.
363 self.assertTrue(user_story_runner._CheckArchives( 370 self.assertTrue(user_story_runner._CheckArchives(ps))
364 ps.archive_data_file, ps.wpr_archive_info, ps.pages))
365 ps.AddPageWithDefaultRunNavigate('http://www.google.com') 371 ps.AddPageWithDefaultRunNavigate('http://www.google.com')
366 # Page set with an archive_data_file which exists but is missing a page. 372 # Page set with an archive_data_file which exists but is missing a page.
367 self.assertFalse(user_story_runner._CheckArchives( 373 self.assertFalse(user_story_runner._CheckArchives(ps))
368 ps.archive_data_file, ps.wpr_archive_info, ps.pages))
369 374
370 ps = page_set.PageSet( 375 ps = page_set.PageSet(
371 archive_data_file='../../unittest_data/test_missing_wpr_file.json', 376 archive_data_file='../../unittest_data/test_missing_wpr_file.json',
372 bucket=page_set.PUBLIC_BUCKET) 377 bucket=page_set.PUBLIC_BUCKET)
373 ps.AddPageWithDefaultRunNavigate('http://www.testurl.com') 378 ps.AddPageWithDefaultRunNavigate('http://www.testurl.com')
374 ps.AddPageWithDefaultRunNavigate('http://www.google.com') 379 ps.AddPageWithDefaultRunNavigate('http://www.google.com')
375 # Page set with an archive_data_file which exists and contains all pages 380 # Page set with an archive_data_file which exists and contains all pages
376 # but fails to find a wpr file. 381 # but fails to find a wpr file.
377 self.assertFalse(user_story_runner._CheckArchives( 382 self.assertFalse(user_story_runner._CheckArchives(ps))
378 ps.archive_data_file, ps.wpr_archive_info, ps.pages))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698