gtest_progress_reporter_unittest.py revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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 sys 6import unittest 7 8from telemetry.core import exceptions 9from telemetry.unittest import gtest_progress_reporter 10from telemetry.unittest import simple_mock 11 12 13try: 14 raise exceptions.IntentionalException() 15except exceptions.IntentionalException: 16 INTENTIONAL_EXCEPTION = sys.exc_info() 17 18 19class TestFoo(unittest.TestCase): 20 # Test method doesn't have test- prefix intentionally. This is so that 21 # run_test script won't run this test. 22 def runTezt(self): 23 pass 24 25 26class TestOutputStream(object): 27 def __init__(self): 28 self._output_data = [] 29 30 @property 31 def output_data(self): 32 return ''.join(self._output_data) 33 34 def write(self, data): 35 self._output_data.append(data) 36 37 def flush(self): 38 pass 39 40 41class TestResultWithSuccesses(unittest.TestResult): 42 def __init__(self): 43 super(TestResultWithSuccesses, self).__init__() 44 self.successes = [] 45 46 def addSuccess(self, test): 47 super(TestResultWithSuccesses, self).addSuccess(test) 48 self.successes.append(test) 49 50 51class GTestProgressReporterTest(unittest.TestCase): 52 def setUp(self): 53 super(GTestProgressReporterTest, self).setUp() 54 self._stream = TestOutputStream() 55 self._formatter = gtest_progress_reporter.GTestProgressReporter( 56 self._stream) 57 58 self._mock_timer = simple_mock.MockTimer() 59 self._real_time_time = gtest_progress_reporter.time.time 60 gtest_progress_reporter.time.time = self._mock_timer.GetTime 61 62 def tearDown(self): 63 gtest_progress_reporter.time.time = self._real_time_time 64 65 def testTestSuiteWithWrapperSuite(self): 66 suite = unittest.TestSuite() 67 suite.addTest(unittest.TestSuite()) 68 self._formatter.StartTestSuite(suite) 69 self._formatter.StopTestSuite(suite) 70 71 self.assertEqual(self._stream.output_data, '') 72 73 def testTestSuiteWithTestCase(self): 74 suite = unittest.TestSuite() 75 suite.addTest(TestFoo(methodName='runTezt')) 76 self._formatter.StartTestSuite(suite) 77 self._mock_timer.SetTime(0.042) 78 self._formatter.StopTestSuite(suite) 79 80 expected = ('[----------] 1 test\n' 81 '[----------] 1 test (42 ms total)\n\n') 82 self.assertEqual(self._stream.output_data, expected) 83 84 def testCaseFailure(self): 85 test = TestFoo(methodName='runTezt') 86 self._formatter.StartTest(test) 87 self._mock_timer.SetTime(0.042) 88 self._formatter.Failure(test, INTENTIONAL_EXCEPTION) 89 90 expected = ( 91 '[ RUN ] gtest_progress_reporter_unittest.TestFoo.runTezt\n' 92 '[ FAILED ] gtest_progress_reporter_unittest.TestFoo.runTezt ' 93 '(42 ms)\n') 94 self.assertEqual(self._stream.output_data, expected) 95 96 def testCaseSuccess(self): 97 test = TestFoo(methodName='runTezt') 98 self._formatter.StartTest(test) 99 self._mock_timer.SetTime(0.042) 100 self._formatter.Success(test) 101 102 expected = ( 103 '[ RUN ] gtest_progress_reporter_unittest.TestFoo.runTezt\n' 104 '[ OK ] gtest_progress_reporter_unittest.TestFoo.runTezt ' 105 '(42 ms)\n') 106 self.assertEqual(self._stream.output_data, expected) 107 108 def testStopTestRun(self): 109 result = TestResultWithSuccesses() 110 self._formatter.StopTestRun(result) 111 112 expected = '[ PASSED ] 0 tests.\n\n' 113 self.assertEqual(self._stream.output_data, expected) 114 115 def testStopTestRunWithFailureAndSuccess(self): 116 test = TestFoo(methodName='runTezt') 117 result = TestResultWithSuccesses() 118 result.addSuccess(test) 119 result.addFailure(test, INTENTIONAL_EXCEPTION) 120 self._formatter.StopTestRun(result) 121 122 expected = ( 123 '[ PASSED ] 1 test.\n' 124 '[ FAILED ] 1 test, listed below:\n' 125 '[ FAILED ] gtest_progress_reporter_unittest.TestFoo.runTezt\n\n' 126 '1 FAILED TEST\n\n') 127 self.assertEqual(self._stream.output_data, expected) 128