Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / results / gtest_progress_reporter.py
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
5 import time
6
7 from telemetry.results import progress_reporter
8 from telemetry.value import failure
9 from telemetry.value import skip
10
11
12 class GTestProgressReporter(progress_reporter.ProgressReporter):
13   """A progress reporter that outputs the progress report in gtest style.
14
15   Be careful each print should only handle one string. Otherwise, the output
16   might be interrupted by Chrome logging, and the output interpretation might
17   be incorrect. For example:
18       print >> self._output_stream, "[ OK ]", testname
19   should be written as
20       print >> self._output_stream, "[ OK ] %s" % testname
21   """
22
23   def __init__(self, output_stream, output_skipped_tests_summary=False):
24     super(GTestProgressReporter, self).__init__()
25     self._output_stream = output_stream
26     self._timestamp = None
27     self._output_skipped_tests_summary = output_skipped_tests_summary
28
29   def _GetMs(self):
30     assert self._timestamp is not None, 'Did not call WillRunPage.'
31     return (time.time() - self._timestamp) * 1000
32
33   def DidAddValue(self, value):
34     super(GTestProgressReporter, self).DidAddValue(value)
35     if isinstance(value, failure.FailureValue):
36       print >> self._output_stream, failure.GetStringFromExcInfo(
37           value.exc_info)
38       self._output_stream.flush()
39     elif isinstance(value, skip.SkipValue):
40       print >> self._output_stream, '===== SKIPPING TEST %s: %s =====' % (
41           value.page.display_name, value.reason)
42     # TODO(chrishenry): Consider outputting metric values as well. For
43     # e.g., it can replace BuildbotOutputFormatter in
44     # --output-format=html, which we used only so that users can grep
45     # the results without opening results.html.
46
47   def WillRunPage(self, page_test_results):
48     super(GTestProgressReporter, self).WillRunPage(page_test_results)
49     print >> self._output_stream, '[ RUN      ] %s' % (
50         page_test_results.current_page.display_name)
51     self._output_stream.flush()
52     self._timestamp = time.time()
53
54   def DidRunPage(self, page_test_results):
55     super(GTestProgressReporter, self).DidRunPage(page_test_results)
56     page = page_test_results.current_page
57     if page_test_results.current_page_run.failed:
58       print >> self._output_stream, '[  FAILED  ] %s (%0.f ms)' % (
59           page.display_name, self._GetMs())
60     else:
61       print >> self._output_stream, '[       OK ] %s (%0.f ms)' % (
62           page.display_name, self._GetMs())
63     self._output_stream.flush()
64
65   def DidFinishAllTests(self, page_test_results):
66     super(GTestProgressReporter, self).DidFinishAllTests(page_test_results)
67     successful_runs = []
68     failed_runs = []
69     for run in page_test_results.all_page_runs:
70       if run.failed:
71         failed_runs.append(run)
72       else:
73         successful_runs.append(run)
74
75     unit = 'test' if len(successful_runs) == 1 else 'tests'
76     print >> self._output_stream, '[  PASSED  ] %d %s.' % (
77         (len(successful_runs), unit))
78     if len(failed_runs) > 0:
79       unit = 'test' if len(failed_runs) == 1 else 'tests'
80       print >> self._output_stream, '[  FAILED  ] %d %s, listed below:' % (
81           (len(page_test_results.failures), unit))
82       for failed_run in failed_runs:
83         print >> self._output_stream, '[  FAILED  ]  %s' % (
84             failed_run.page.display_name)
85       print >> self._output_stream
86       count = len(failed_runs)
87       unit = 'TEST' if count == 1 else 'TESTS'
88       print >> self._output_stream, '%d FAILED %s' % (count, unit)
89     print >> self._output_stream
90
91     if self._output_skipped_tests_summary:
92       if len(page_test_results.skipped_values) > 0:
93         print >> self._output_stream, 'Skipped pages:\n%s\n' % ('\n'.join(
94             v.page.display_name for v in page_test_results.skipped_values))
95
96     self._output_stream.flush()