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.
8 from telemetry.core import exceptions
9 from telemetry.unittest import gtest_progress_reporter
10 from telemetry.unittest import simple_mock
14 raise exceptions.IntentionalException()
15 except exceptions.IntentionalException:
16 INTENTIONAL_EXCEPTION = sys.exc_info()
19 class 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.
26 class TestOutputStream(object):
28 self._output_data = []
31 def output_data(self):
32 return ''.join(self._output_data)
34 def write(self, data):
35 self._output_data.append(data)
41 class TestResultWithSuccesses(unittest.TestResult):
43 super(TestResultWithSuccesses, self).__init__()
46 def addSuccess(self, test):
47 super(TestResultWithSuccesses, self).addSuccess(test)
48 self.successes.append(test)
51 class GTestProgressReporterTest(unittest.TestCase):
53 super(GTestProgressReporterTest, self).setUp()
54 self._stream = TestOutputStream()
55 self._formatter = gtest_progress_reporter.GTestProgressReporter(
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
63 gtest_progress_reporter.time.time = self._real_time_time
65 def testTestSuiteWithWrapperSuite(self):
66 suite = unittest.TestSuite()
67 suite.addTest(unittest.TestSuite())
68 self._formatter.StartTestSuite(suite)
69 self._formatter.StopTestSuite(suite)
71 self.assertEqual(self._stream.output_data, '')
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)
80 expected = ('[----------] 1 test\n'
81 '[----------] 1 test (42 ms total)\n\n')
82 self.assertEqual(self._stream.output_data, expected)
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)
91 '[ RUN ] gtest_progress_reporter_unittest.TestFoo.runTezt\n'
92 '[ FAILED ] gtest_progress_reporter_unittest.TestFoo.runTezt '
94 self.assertEqual(self._stream.output_data, expected)
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)
103 '[ RUN ] gtest_progress_reporter_unittest.TestFoo.runTezt\n'
104 '[ OK ] gtest_progress_reporter_unittest.TestFoo.runTezt '
106 self.assertEqual(self._stream.output_data, expected)
108 def testStopTestRun(self):
109 result = TestResultWithSuccesses()
110 self._formatter.StopTestRun(result)
112 expected = '[ PASSED ] 0 tests.\n\n'
113 self.assertEqual(self._stream.output_data, expected)
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)
123 '[ PASSED ] 1 test.\n'
124 '[ FAILED ] 1 test, listed below:\n'
125 '[ FAILED ] gtest_progress_reporter_unittest.TestFoo.runTezt\n\n'
127 self.assertEqual(self._stream.output_data, expected)