OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Implements a unittest TestRunner with GTest output. | |
7 | |
8 This output is ported from gtest.cc's PrettyUnitTestResultPrinter, but | |
9 designed to be a drop-in replacement for unittest's TextTestRunner. | |
10 """ | |
11 | |
12 import time | |
13 import unittest | |
14 | |
15 | |
16 class GTestTestResult(unittest.TestResult): | |
17 def __init__(self): | |
18 unittest.TestResult.__init__(self) | |
19 self.timestamp = None | |
20 self.num_successes = 0 | |
21 | |
22 def _GetMs(self): | |
23 return (time.time() - self.timestamp) * 1000 | |
24 | |
25 @property | |
26 def num_errors(self): | |
27 return len(self.errors) + len(self.failures) | |
28 | |
29 @staticmethod | |
30 def _formatTestname(test): | |
31 chunks = test.id().split('.')[-2:] | |
32 return '.'.join(chunks) | |
33 | |
34 def _emitFailure(self, test, err): | |
35 print self._exc_info_to_string(err, test) | |
36 test_name = GTestTestResult._formatTestname(test) | |
37 print '[ FAILED ]', test_name, '(%0.f ms)' % self._GetMs() | |
38 | |
39 def addError(self, test, err): | |
40 self._emitFailure(test, err) | |
41 super(GTestTestResult, self).addError(test, err) | |
42 | |
43 def addFailure(self, test, err): | |
44 self._emitFailure(test, err) | |
45 super(GTestTestResult, self).addFailure(test, err) | |
46 | |
47 def startTest(self, test): | |
48 print '[ RUN ]', GTestTestResult._formatTestname(test) | |
49 self.timestamp = time.time() | |
50 super(GTestTestResult, self).startTest(test) | |
51 | |
52 def addSuccess(self, test): | |
53 self.num_successes = self.num_successes + 1 | |
54 test_name = GTestTestResult._formatTestname(test) | |
55 print '[ OK ]', test_name, '(%0.f ms)' % self._GetMs() | |
56 | |
57 def PrintSummary(self): | |
58 unit = 'test' if self.num_successes == 1 else 'tests' | |
59 print '[ PASSED ] %d %s.' % (self.num_successes, unit) | |
60 if self.errors or self.failures: | |
61 all_errors = self.errors[:] | |
62 all_errors.extend(self.failures) | |
63 unit = 'test' if len(all_errors) == 1 else 'tests' | |
64 print '[ FAILED ] %d %s, listed below:' % (len(all_errors), unit) | |
65 for test, _ in all_errors: | |
66 print '[ FAILED ] ', GTestTestResult._formatTestname(test) | |
67 if not self.wasSuccessful(): | |
68 print | |
69 count = len(self.errors) + len(self.failures) | |
70 unit = 'TEST' if count == 1 else 'TESTS' | |
71 print '%d FAILED %s' % (count, unit) | |
72 print | |
73 | |
74 | |
75 class GTestTestSuite(unittest.TestSuite): | |
76 def __call__(self, *args, **kwargs): | |
77 result = args[0] | |
78 timestamp = time.time() | |
79 unit = 'test' if len(self._tests) == 1 else 'tests' | |
80 if not any(isinstance(x, unittest.TestSuite) for x in self._tests): | |
81 print '[----------] %d %s' % (len(self._tests), unit) | |
82 for test in self._tests: | |
83 if result.shouldStop: | |
84 break | |
85 test(result) | |
86 endts = time.time() | |
87 ms = (endts - timestamp) * 1000 | |
88 if not any(isinstance(x, unittest.TestSuite) for x in self._tests): | |
89 print '[----------] %d %s (%d ms total)' % (len(self._tests), unit, ms) | |
90 print | |
91 return result | |
92 | |
93 | |
94 class GTestTestRunner(object): | |
95 def __init__(self, print_result_after_run=True, runner=None): | |
96 self.print_result_after_run = print_result_after_run | |
97 self.result = None | |
98 if runner: | |
99 self.result = runner.result | |
100 | |
101 def run(self, test): | |
102 "Run the given test case or test suite." | |
103 if not self.result: | |
104 self.result = GTestTestResult() | |
105 test(self.result) | |
106 if self.print_result_after_run: | |
107 self.result.PrintSummary() | |
108 return self.result | |
OLD | NEW |