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 logging 6import sys 7import time 8import unittest 9 10 11class GTestUnittestResults(unittest.TestResult): 12 def __init__(self, output_stream): 13 super(GTestUnittestResults, self).__init__() 14 self._output_stream = output_stream 15 self._timestamp = None 16 self._successes_count = 0 17 18 def _GetMs(self): 19 return (time.time() - self._timestamp) * 1000 20 21 @property 22 def num_errors(self): 23 return len(self.errors) + len(self.failures) 24 25 @staticmethod 26 def _formatTestname(test): 27 chunks = test.id().split('.')[2:] 28 return '.'.join(chunks) 29 30 def _emitFailure(self, test, err): 31 print >> self._output_stream, self._exc_info_to_string(err, test) 32 test_name = GTestUnittestResults._formatTestname(test) 33 print >> self._output_stream, '[ FAILED ]', test_name, ( 34 '(%0.f ms)' % self._GetMs()) 35 sys.stdout.flush() 36 37 def addError(self, test, err): 38 super(GTestUnittestResults, self).addError(test, err) 39 self._emitFailure(test, err) 40 41 def addFailure(self, test, err): 42 super(GTestUnittestResults, self).addFailure(test, err) 43 self._emitFailure(test, err) 44 45 def startTest(self, test): 46 super(GTestUnittestResults, self).startTest(test) 47 print >> self._output_stream, '[ RUN ]', ( 48 GTestUnittestResults._formatTestname(test)) 49 sys.stdout.flush() 50 self._timestamp = time.time() 51 52 def addSuccess(self, test): 53 super(GTestUnittestResults, self).addSuccess(test) 54 self._successes_count += 1 55 test_name = GTestUnittestResults._formatTestname(test) 56 print >> self._output_stream, '[ OK ]', test_name, ( 57 '(%0.f ms)' % self._GetMs()) 58 sys.stdout.flush() 59 60 def addSkip(self, test, reason): 61 super(GTestUnittestResults, self).addSkip(test, reason) 62 test_name = GTestUnittestResults._formatTestname(test) 63 logging.warning('===== SKIPPING TEST %s: %s =====', test_name, reason) 64 if self._timestamp == None: 65 self._timestamp = time.time() 66 print >> self._output_stream, '[ OK ]', test_name, ( 67 '(%0.f ms)' % self._GetMs()) 68 sys.stdout.flush() 69 70 def PrintSummary(self): 71 unit = 'test' if self._successes_count == 1 else 'tests' 72 print >> self._output_stream, '[ PASSED ]', ( 73 '%d %s.' % (self._successes_count, unit)) 74 if self.errors or self.failures: 75 all_errors = self.errors[:] 76 all_errors.extend(self.failures) 77 unit = 'test' if len(all_errors) == 1 else 'tests' 78 print >> self._output_stream, '[ FAILED ]', ( 79 '%d %s, listed below:' % (len(all_errors), unit)) 80 for test, _ in all_errors: 81 print >> self._output_stream, '[ FAILED ] ', ( 82 GTestUnittestResults._formatTestname(test)) 83 if not self.wasSuccessful(): 84 print >> self._output_stream 85 count = len(self.errors) + len(self.failures) 86 unit = 'TEST' if count == 1 else 'TESTS' 87 print >> self._output_stream, '%d FAILED %s' % (count, unit) 88 print >> self._output_stream 89 sys.stdout.flush() 90