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(gtest_progress_reporter)
61 self._mock_timer.Restore()
63 def testTestSuiteWithWrapperSuite(self):
64 suite = unittest.TestSuite()
65 suite.addTest(unittest.TestSuite())
66 self._formatter.StartTestSuite(suite)
67 self._formatter.StopTestSuite(suite)
69 self.assertEqual(self._stream.output_data, '')
71 def testTestSuiteWithTestCase(self):
72 suite = unittest.TestSuite()
73 suite.addTest(TestFoo(methodName='runTezt'))
74 self._formatter.StartTestSuite(suite)
75 self._mock_timer.SetTime(0.042)
76 self._formatter.StopTestSuite(suite)
78 expected = ('[----------] 1 test\n'
79 '[----------] 1 test (42 ms total)\n\n')
80 self.assertEqual(self._stream.output_data, expected)
82 def testCaseFailure(self):
83 test = TestFoo(methodName='runTezt')
84 self._formatter.StartTest(test)
85 self._mock_timer.SetTime(0.042)
86 self._formatter.Failure(test, INTENTIONAL_EXCEPTION)
89 '[ RUN ] gtest_progress_reporter_unittest.TestFoo.runTezt\n'
90 '[ FAILED ] gtest_progress_reporter_unittest.TestFoo.runTezt '
92 self.assertEqual(self._stream.output_data, expected)
94 def testCaseSuccess(self):
95 test = TestFoo(methodName='runTezt')
96 self._formatter.StartTest(test)
97 self._mock_timer.SetTime(0.042)
98 self._formatter.Success(test)
101 '[ RUN ] gtest_progress_reporter_unittest.TestFoo.runTezt\n'
102 '[ OK ] gtest_progress_reporter_unittest.TestFoo.runTezt '
104 self.assertEqual(self._stream.output_data, expected)
106 def testStopTestRun(self):
107 result = TestResultWithSuccesses()
108 self._formatter.StopTestRun(result)
110 expected = '[ PASSED ] 0 tests.\n\n'
111 self.assertEqual(self._stream.output_data, expected)
113 def testStopTestRunWithFailureAndSuccess(self):
114 test = TestFoo(methodName='runTezt')
115 result = TestResultWithSuccesses()
116 result.addSuccess(test)
117 result.addFailure(test, INTENTIONAL_EXCEPTION)
118 self._formatter.StopTestRun(result)
121 '[ PASSED ] 1 test.\n'
122 '[ FAILED ] 1 test, listed below:\n'
123 '[ FAILED ] gtest_progress_reporter_unittest.TestFoo.runTezt\n\n'
125 self.assertEqual(self._stream.output_data, expected)