- add sources.
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / page / gtest_test_results.py
1 # Copyright (c) 2013 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 sys
6 import time
7 import unittest
8
9 from telemetry.page import page_test_results
10
11 class GTestTestResults(page_test_results.PageTestResults):
12   def __init__(self, output_stream):
13     super(GTestTestResults, self).__init__()
14     self._output_stream = output_stream
15     self._timestamp = None
16
17   def _GetMs(self):
18     return (time.time() - self._timestamp) * 1000
19
20   @property
21   def num_errors(self):
22     return len(self.errors) + len(self.failures)
23
24   @staticmethod
25   def _formatTestname(test):
26     if isinstance(test, unittest.TestCase):
27       chunks = test.id().split('.')[-2:]
28       return '.'.join(chunks)
29     else:
30       return str(test)
31
32   def _emitFailure(self, test, err):
33     print >> self._output_stream, self._exc_info_to_string(err, test)
34     test_name = GTestTestResults._formatTestname(test)
35     print >> self._output_stream, '[  FAILED  ]', test_name, (
36         '(%0.f ms)' % self._GetMs())
37     sys.stdout.flush()
38
39   def addError(self, test, err):
40     super(GTestTestResults, self).addError(test, err)
41     self._emitFailure(test, err)
42
43   def addFailure(self, test, err):
44     super(GTestTestResults, self).addFailure(test, err)
45     self._emitFailure(test, err)
46
47   def startTest(self, test):
48     super(GTestTestResults, self).startTest(test)
49     print >> self._output_stream, '[ RUN      ]', (
50         GTestTestResults._formatTestname(test))
51     sys.stdout.flush()
52     self._timestamp = time.time()
53
54   def addSuccess(self, test):
55     super(GTestTestResults, self).addSuccess(test)
56     test_name = GTestTestResults._formatTestname(test)
57     print >> self._output_stream, '[       OK ]', test_name, (
58         '(%0.f ms)' % self._GetMs())
59     sys.stdout.flush()
60
61   def PrintSummary(self):
62     unit = 'test' if len(self.successes) == 1 else 'tests'
63     print >> self._output_stream, '[  PASSED  ]', (
64         '%d %s.' % (len(self.successes), unit))
65     if self.errors or self.failures:
66       all_errors = self.errors[:]
67       all_errors.extend(self.failures)
68       unit = 'test' if len(all_errors) == 1 else 'tests'
69       print >> self._output_stream, '[  FAILED  ]', (
70           '%d %s, listed below:' % (len(all_errors), unit))
71       for test, _ in all_errors:
72         print >> self._output_stream, '[  FAILED  ] ', (
73             GTestTestResults._formatTestname(test))
74     if not self.wasSuccessful():
75       print >> self._output_stream
76       count = len(self.errors) + len(self.failures)
77       unit = 'TEST' if count == 1 else 'TESTS'
78       print >> self._output_stream, '%d FAILED %s' % (count, unit)
79     print >> self._output_stream
80     sys.stdout.flush()