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_progress_reporter | |
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 GTestProgressReporterTest(unittest.TestCase): | |
53 def setUp(self): | |
54 super(GTestProgressReporterTest, self).setUp() | |
55 self._stream = TestOutputStream() | |
56 self._formatter = gtest_progress_reporter.GTestProgressReporter( | |
57 self._stream) | |
58 | |
59 self._mock_timer = simple_mock.MockTimer() | |
60 self._real_time_time = gtest_progress_reporter.time.time | |
61 gtest_progress_reporter.time.time = self._mock_timer.GetTime | |
62 | |
63 def tearDown(self): | |
64 gtest_progress_reporter.time.time = self._real_time_time | |
65 | |
66 def testTestSuiteWithWrapperSuite(self): | |
67 suite = unittest.TestSuite() | |
68 suite.addTest(unittest.TestSuite()) | |
69 self._formatter.StartTestSuite(suite) | |
70 self._formatter.StopTestSuite(suite) | |
71 | |
72 self.assertEqual(self._stream.output_data, '') | |
73 | |
74 def testTestSuiteWithTestCase(self): | |
75 suite = unittest.TestSuite() | |
76 suite.addTest(TestFoo(methodName='runTezt')) | |
77 self._formatter.StartTestSuite(suite) | |
78 self._mock_timer.SetTime(0.042) | |
79 self._formatter.StopTestSuite(suite) | |
80 | |
81 expected = ('[----------] 1 test\n' | |
82 '[----------] 1 test (42 ms total)\n\n') | |
83 self.assertEqual(self._stream.output_data, expected) | |
84 | |
85 def testCaseFailure(self): | |
86 test = TestFoo(methodName='runTezt') | |
87 self._formatter.StartTest(test) | |
88 self._mock_timer.SetTime(0.042) | |
89 self._formatter.Failure(test, DUMMY_EXCEPTION) | |
90 | |
91 expected = ( | |
92 '[ RUN ] gtest_progress_reporter_unittest.TestFoo.runTezt\n' | |
93 '[ FAILED ] gtest_progress_reporter_unittest.TestFoo.runTezt ' | |
94 '(42 ms)\n') | |
95 self.assertEqual(self._stream.output_data, expected) | |
96 | |
97 def testCaseSuccess(self): | |
98 test = TestFoo(methodName='runTezt') | |
99 self._formatter.StartTest(test) | |
100 self._mock_timer.SetTime(0.042) | |
101 self._formatter.Success(test) | |
102 | |
103 expected = ( | |
104 '[ RUN ] gtest_progress_reporter_unittest.TestFoo.runTezt\n' | |
105 '[ OK ] gtest_progress_reporter_unittest.TestFoo.runTezt ' | |
106 '(42 ms)\n') | |
107 self.assertEqual(self._stream.output_data, expected) | |
108 | |
109 def testStopTestRun(self): | |
110 result = TestResultWithSuccesses() | |
111 self._formatter.StopTestRun(result) | |
112 | |
113 expected = '[ PASSED ] 0 tests.\n\n' | |
114 self.assertEqual(self._stream.output_data, expected) | |
115 | |
116 def testStopTestRunWithFailureAndSuccess(self): | |
117 test = TestFoo(methodName='runTezt') | |
118 result = TestResultWithSuccesses() | |
119 result.addSuccess(test) | |
120 result.addFailure(test, DUMMY_EXCEPTION) | |
121 self._formatter.StopTestRun(result) | |
122 | |
123 expected = ( | |
124 '[ PASSED ] 1 test.\n' | |
125 '[ FAILED ] 1 test, listed below:\n' | |
126 '[ FAILED ] gtest_progress_reporter_unittest.TestFoo.runTezt\n\n' | |
127 '1 FAILED TEST\n\n') | |
128 self.assertEqual(self._stream.output_data, expected) | |
OLD | NEW |