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 5import time 6import sys 7 8from tracing.mre import progress_reporter 9 10 11class GTestRunReporter(progress_reporter.RunReporter): 12 13 def __init__(self, canonical_url, output_stream, timestamp): 14 super(GTestRunReporter, self).__init__(canonical_url) 15 self._output_stream = output_stream 16 self._timestamp = timestamp 17 18 def _GetMs(self): 19 assert self._timestamp is not None, 'Did not call WillRun.' 20 return (time.time() - self._timestamp) * 1000 21 22 def DidAddFailure(self, failure): 23 super(GTestRunReporter, self).DidAddFailure(failure) 24 print >> self._output_stream, failure.stack 25 self._output_stream.flush() 26 27 def DidRun(self, run_failed): 28 super(GTestRunReporter, self).DidRun(run_failed) 29 if run_failed: 30 print >> self._output_stream, '[ FAILED ] %s (%0.f ms)' % ( 31 self.canonical_url, self._GetMs()) 32 else: 33 print >> self._output_stream, '[ OK ] %s (%0.f ms)' % ( 34 self.canonical_url, self._GetMs()) 35 self._output_stream.flush() 36 37 38class GTestProgressReporter(progress_reporter.ProgressReporter): 39 """A progress reporter that outputs the progress report in gtest style. 40 41 Be careful each print should only handle one string. Otherwise, the output 42 might be interrupted by Chrome logging, and the output interpretation might 43 be incorrect. For example: 44 print >> self._output_stream, "[ OK ]", testname 45 should be written as 46 print >> self._output_stream, "[ OK ] %s" % testname 47 """ 48 49 def __init__(self, output_stream=sys.stdout): 50 super(GTestProgressReporter, self).__init__() 51 self._output_stream = output_stream 52 53 def WillRun(self, canonical_url): 54 super(GTestProgressReporter, self).WillRun(canonical_url) 55 print >> self._output_stream, '[ RUN ] %s' % (canonical_url) 56 self._output_stream.flush() 57 return GTestRunReporter(canonical_url, self._output_stream, time.time()) 58 59 def DidFinishAllRuns(self, result_list): 60 super(GTestProgressReporter, self).DidFinishAllRuns(result_list) 61 successful_runs = 0 62 failed_canonical_urls = [] 63 failed_runs = 0 64 for run in result_list: 65 if len(run.failures) != 0: 66 failed_runs += 1 67 for f in run.failures: 68 failed_canonical_urls.append(f.trace_canonical_url) 69 else: 70 successful_runs += 1 71 72 unit = 'test' if successful_runs == 1 else 'tests' 73 print >> self._output_stream, '[ PASSED ] %d %s.' % ( 74 (successful_runs, unit)) 75 if len(failed_canonical_urls) > 0: 76 unit = 'test' if len(failed_canonical_urls) == 1 else 'tests' 77 print >> self._output_stream, '[ FAILED ] %d %s, listed below:' % ( 78 (failed_runs, unit)) 79 for failed_canonical_url in failed_canonical_urls: 80 print >> self._output_stream, '[ FAILED ] %s' % ( 81 failed_canonical_url) 82 print >> self._output_stream 83 count = len(failed_canonical_urls) 84 unit = 'TEST' if count == 1 else 'TESTS' 85 print >> self._output_stream, '%d FAILED %s' % (count, unit) 86 print >> self._output_stream 87 88 self._output_stream.flush() 89