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