OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2006 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 """ | |
5 Test the interaction between trial and errors logged during test run. | |
6 """ | |
7 | |
8 import time | |
9 | |
10 from twisted.internet import defer | |
11 from twisted.python import failure, log | |
12 from twisted.trial import unittest, reporter, runner | |
13 | |
14 | |
15 def makeFailure(): | |
16 """ | |
17 Return a new, realistic failure. | |
18 """ | |
19 try: | |
20 1/0 | |
21 except ZeroDivisionError: | |
22 f = failure.Failure() | |
23 return f | |
24 | |
25 | |
26 def deferLater(n): | |
27 """ | |
28 Return a L{defer.Deferred} that will fire in C{n} seconds. | |
29 """ | |
30 from twisted.internet import reactor | |
31 d = defer.Deferred() | |
32 reactor.callLater(n, d.callback, None) | |
33 return d | |
34 | |
35 | |
36 class Mask(object): | |
37 """ | |
38 Hide C{MockTest}s from Trial's automatic test finder. | |
39 """ | |
40 | |
41 class MockTest(unittest.TestCase): | |
42 def test_silent(self): | |
43 """ | |
44 Don't log any errors. | |
45 """ | |
46 | |
47 def test_single(self): | |
48 """ | |
49 Log a single error. | |
50 """ | |
51 log.err(makeFailure()) | |
52 | |
53 def test_double(self): | |
54 """ | |
55 Log two errors. | |
56 """ | |
57 log.err(makeFailure()) | |
58 log.err(makeFailure()) | |
59 | |
60 def test_inCallback(self): | |
61 """ | |
62 Log an error in an asynchronous callback. | |
63 """ | |
64 d = deferLater(0) | |
65 d.addCallback(lambda _: log.err(makeFailure())) | |
66 return d | |
67 | |
68 | |
69 class TestObserver(unittest.TestCase): | |
70 def setUp(self): | |
71 self.result = reporter.TestResult() | |
72 self.observer = unittest._LogObserver() | |
73 | |
74 def test_msg(self): | |
75 """ | |
76 Test that a standard log message doesn't go anywhere near the result. | |
77 """ | |
78 self.observer.gotEvent({'message': ('some message',), | |
79 'time': time.time(), 'isError': 0, | |
80 'system': '-'}) | |
81 self.assertEqual(self.observer.getErrors(), []) | |
82 | |
83 def test_error(self): | |
84 """ | |
85 Test that an observed error gets added to the result | |
86 """ | |
87 f = makeFailure() | |
88 self.observer.gotEvent({'message': (), | |
89 'time': time.time(), 'isError': 1, | |
90 'system': '-', 'failure': f, | |
91 'why': None}) | |
92 self.assertEqual(self.observer.getErrors(), [f]) | |
93 | |
94 def test_flush(self): | |
95 """ | |
96 Check that flushing the observer with no args removes all errors. | |
97 """ | |
98 self.test_error() | |
99 flushed = self.observer.flushErrors() | |
100 self.assertEqual(self.observer.getErrors(), []) | |
101 self.assertEqual(len(flushed), 1) | |
102 self.assertTrue(flushed[0].check(ZeroDivisionError)) | |
103 | |
104 def _makeRuntimeFailure(self): | |
105 return failure.Failure(RuntimeError('test error')) | |
106 | |
107 def test_flushByType(self): | |
108 """ | |
109 Check that flushing the observer remove all failures of the given type. | |
110 """ | |
111 self.test_error() # log a ZeroDivisionError to the observer | |
112 f = self._makeRuntimeFailure() | |
113 self.observer.gotEvent(dict(message=(), time=time.time(), isError=1, | |
114 system='-', failure=f, why=None)) | |
115 flushed = self.observer.flushErrors(ZeroDivisionError) | |
116 self.assertEqual(self.observer.getErrors(), [f]) | |
117 self.assertEqual(len(flushed), 1) | |
118 self.assertTrue(flushed[0].check(ZeroDivisionError)) | |
119 | |
120 def test_ignoreErrors(self): | |
121 """ | |
122 Check that C{_ignoreErrors} actually causes errors to be ignored. | |
123 """ | |
124 self.observer._ignoreErrors(ZeroDivisionError) | |
125 f = makeFailure() | |
126 self.observer.gotEvent({'message': (), | |
127 'time': time.time(), 'isError': 1, | |
128 'system': '-', 'failure': f, | |
129 'why': None}) | |
130 self.assertEqual(self.observer.getErrors(), []) | |
131 | |
132 def test_clearIgnores(self): | |
133 """ | |
134 Check that C{_clearIgnores} ensures that previously ignored errors | |
135 get captured. | |
136 """ | |
137 self.observer._ignoreErrors(ZeroDivisionError) | |
138 self.observer._clearIgnores() | |
139 f = makeFailure() | |
140 self.observer.gotEvent({'message': (), | |
141 'time': time.time(), 'isError': 1, | |
142 'system': '-', 'failure': f, | |
143 'why': None}) | |
144 self.assertEqual(self.observer.getErrors(), [f]) | |
145 | |
146 | |
147 class LogErrors(unittest.TestCase): | |
148 """ | |
149 High-level tests demonstrating the expected behaviour of logged errors | |
150 during tests. | |
151 """ | |
152 | |
153 def setUp(self): | |
154 self.result = reporter.TestResult() | |
155 | |
156 def tearDown(self): | |
157 self.flushLoggedErrors(ZeroDivisionError) | |
158 | |
159 def test_singleError(self): | |
160 """ | |
161 Test that a logged error gets reported as a test error. | |
162 """ | |
163 test = Mask.MockTest('test_single') | |
164 test(self.result) | |
165 self.assertEqual(len(self.result.errors), 1) | |
166 self.assertTrue(self.result.errors[0][1].check(ZeroDivisionError), | |
167 self.result.errors[0][1]) | |
168 | |
169 def test_twoErrors(self): | |
170 """ | |
171 Test that when two errors get logged, they both get reported as test | |
172 errors. | |
173 """ | |
174 test = Mask.MockTest('test_double') | |
175 test(self.result) | |
176 self.assertEqual(len(self.result.errors), 2) | |
177 | |
178 def test_inCallback(self): | |
179 """ | |
180 Test that errors logged in callbacks get reported as test errors. | |
181 """ | |
182 test = Mask.MockTest('test_inCallback') | |
183 test(self.result) | |
184 self.assertEqual(len(self.result.errors), 1) | |
185 self.assertTrue(self.result.errors[0][1].check(ZeroDivisionError), | |
186 self.result.errors[0][1]) | |
187 | |
188 def test_errorsIsolated(self): | |
189 """ | |
190 Check that an error logged in one test doesn't fail the next test. | |
191 """ | |
192 t1 = Mask.MockTest('test_single') | |
193 t2 = Mask.MockTest('test_silent') | |
194 t1(self.result) | |
195 t2(self.result) | |
196 self.assertEqual(len(self.result.errors), 1) | |
197 self.assertEqual(self.result.errors[0][0], t1) | |
OLD | NEW |