OLD | NEW |
(Empty) | |
| 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 |
| 5 import unittest |
| 6 import sys |
| 7 |
| 8 from telemetry.unittest import gtest_output_formatter |
| 9 from telemetry.unittest import simple_mock |
| 10 |
| 11 |
| 12 class DummyException(Exception): |
| 13 pass |
| 14 try: |
| 15 raise DummyException('Dummy exception') |
| 16 except DummyException: |
| 17 DUMMY_EXCEPTION = sys.exc_info() |
| 18 |
| 19 |
| 20 class 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 |
| 27 class TestOutputStream(object): |
| 28 def __init__(self): |
| 29 self._output_data = [] |
| 30 |
| 31 @property |
| 32 def output_data(self): |
| 33 return ''.join(self._output_data) |
| 34 |
| 35 def write(self, data): |
| 36 self._output_data.append(data) |
| 37 |
| 38 def flush(self): |
| 39 pass |
| 40 |
| 41 |
| 42 class TestResultWithSuccesses(unittest.TestResult): |
| 43 def __init__(self): |
| 44 super(TestResultWithSuccesses, self).__init__() |
| 45 self.successes = [] |
| 46 |
| 47 def addSuccess(self, test): |
| 48 super(TestResultWithSuccesses, self).addSuccess(test) |
| 49 self.successes.append(test) |
| 50 |
| 51 |
| 52 class GTestOutputFormatterTest(unittest.TestCase): |
| 53 def setUp(self): |
| 54 super(GTestOutputFormatterTest, self).setUp() |
| 55 self._stream = TestOutputStream() |
| 56 self._formatter = gtest_output_formatter.GTestOutputFormatter(self._stream) |
| 57 |
| 58 self._mock_timer = simple_mock.MockTimer() |
| 59 self._real_time_time = gtest_output_formatter.time.time |
| 60 gtest_output_formatter.time.time = self._mock_timer.GetTime |
| 61 |
| 62 def tearDown(self): |
| 63 gtest_output_formatter.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, DUMMY_EXCEPTION) |
| 89 |
| 90 expected = ('[ RUN ] gtest_output_formatter_unittest.TestFoo.runTezt\n' |
| 91 '[ FAILED ] gtest_output_formatter_unittest.TestFoo.runTezt ' |
| 92 '(42 ms)\n') |
| 93 self.assertEqual(self._stream.output_data, expected) |
| 94 |
| 95 def testCaseSuccess(self): |
| 96 test = TestFoo(methodName='runTezt') |
| 97 self._formatter.StartTest(test) |
| 98 self._mock_timer.SetTime(0.042) |
| 99 self._formatter.Success(test) |
| 100 |
| 101 expected = ('[ RUN ] gtest_output_formatter_unittest.TestFoo.runTezt\n' |
| 102 '[ OK ] gtest_output_formatter_unittest.TestFoo.runTezt ' |
| 103 '(42 ms)\n') |
| 104 self.assertEqual(self._stream.output_data, expected) |
| 105 |
| 106 def testStopTestRun(self): |
| 107 result = TestResultWithSuccesses() |
| 108 self._formatter.StopTestRun(result) |
| 109 |
| 110 expected = '[ PASSED ] 0 tests.\n\n' |
| 111 self.assertEqual(self._stream.output_data, expected) |
OLD | NEW |