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

Side by Side Diff: tools/telemetry/telemetry/test/gtest_testrunner.py

Issue 12294002: Revert 182991 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 10 months 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 | Annotate | Revision Log
OLDNEW
(Empty)
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 """Implements a unittest TestRunner with GTest output.
6
7 This output is ported from gtest.cc's PrettyUnitTestResultPrinter, but
8 designed to be a drop-in replacement for unittest's TextTestRunner.
9 """
10
11 import time
12 import unittest
13
14
15 class GTestTestResult(unittest.TestResult):
16 def __init__(self):
17 unittest.TestResult.__init__(self)
18 self.timestamp = None
19 self.num_successes = 0
20
21 def _GetMs(self):
22 return (time.time() - self.timestamp) * 1000
23
24 @property
25 def num_errors(self):
26 return len(self.errors) + len(self.failures)
27
28 @staticmethod
29 def _formatTestname(test):
30 chunks = test.id().split('.')[-2:]
31 return '.'.join(chunks)
32
33 def _emitFailure(self, test, err):
34 print self._exc_info_to_string(err, test)
35 test_name = GTestTestResult._formatTestname(test)
36 print '[ FAILED ]', test_name, '(%0.f ms)' % self._GetMs()
37
38 def addError(self, test, err):
39 self._emitFailure(test, err)
40 super(GTestTestResult, self).addError(test, err)
41
42 def addFailure(self, test, err):
43 self._emitFailure(test, err)
44 super(GTestTestResult, self).addFailure(test, err)
45
46 def startTest(self, test):
47 print '[ RUN ]', GTestTestResult._formatTestname(test)
48 self.timestamp = time.time()
49 super(GTestTestResult, self).startTest(test)
50
51 def addSuccess(self, test):
52 self.num_successes = self.num_successes + 1
53 test_name = GTestTestResult._formatTestname(test)
54 print '[ OK ]', test_name, '(%0.f ms)' % self._GetMs()
55
56 def PrintSummary(self):
57 unit = 'test' if self.num_successes == 1 else 'tests'
58 print '[ PASSED ] %d %s.' % (self.num_successes, unit)
59 if self.errors or self.failures:
60 all_errors = self.errors[:]
61 all_errors.extend(self.failures)
62 unit = 'test' if len(all_errors) == 1 else 'tests'
63 print '[ FAILED ] %d %s, listed below:' % (len(all_errors), unit)
64 for test, _ in all_errors:
65 print '[ FAILED ] ', GTestTestResult._formatTestname(test)
66 if not self.wasSuccessful():
67 print
68 count = len(self.errors) + len(self.failures)
69 unit = 'TEST' if count == 1 else 'TESTS'
70 print '%d FAILED %s' % (count, unit)
71 print
72
73
74 class GTestTestSuite(unittest.TestSuite):
75 def __call__(self, *args, **kwargs):
76 result = args[0]
77 timestamp = time.time()
78 unit = 'test' if len(self._tests) == 1 else 'tests'
79 if not any(isinstance(x, unittest.TestSuite) for x in self._tests):
80 print '[----------] %d %s' % (len(self._tests), unit)
81 for test in self._tests:
82 if result.shouldStop:
83 break
84 test(result)
85 endts = time.time()
86 ms = (endts - timestamp) * 1000
87 if not any(isinstance(x, unittest.TestSuite) for x in self._tests):
88 print '[----------] %d %s (%d ms total)' % (len(self._tests), unit, ms)
89 print
90 return result
91
92
93 class GTestTestRunner(object):
94 def __init__(self, print_result_after_run=True, runner=None):
95 self.print_result_after_run = print_result_after_run
96 self.result = None
97 if runner:
98 self.result = runner.result
99
100 def run(self, test):
101 "Run the given test case or test suite."
102 if not self.result:
103 self.result = GTestTestResult()
104 test(self.result)
105 if self.print_result_after_run:
106 self.result.PrintSummary()
107 return self.result
OLDNEW
« no previous file with comments | « tools/telemetry/telemetry/test/discover.py ('k') | tools/telemetry/telemetry/test/options_for_unittests.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698