3
3
"""Tests for extensions to the base test library."""
5
from pyunit3k import TestCase, TestResult, clone_test_with_new_id
8
class LoggingResult(TestResult):
9
"""TestResult that logs its event to a list."""
11
def __init__(self, log):
13
super(LoggingResult, self).__init__()
15
def startTest(self, test):
16
self._events.append('startTest')
17
super(LoggingResult, self).startTest(test)
19
def stopTest(self, test):
20
self._events.append('stopTest')
21
super(LoggingResult, self).stopTest(test)
23
def addFailure(self, *args):
24
self._events.append('addFailure')
25
super(LoggingResult, self).addFailure(*args)
27
def addError(self, *args):
28
self._events.append('addError')
29
super(LoggingResult, self).addError(*args)
31
def addSuccess(self, *args):
32
self._events.append('addSuccess')
33
super(LoggingResult, self).addSuccess(*args)
5
from pyunit3k import TestCase, clone_test_with_new_id
6
from pyunit3k.tests.helpers import LoggingResult
9
class TestEquality(TestCase):
10
"""Test `TestCase`'s equality implementation."""
12
def test_identicalIsEqual(self):
13
# TestCase's are equal if they are identical.
14
self.assertEqual(self, self)
16
def test_nonIdenticalInUnequal(self):
17
# TestCase's are not equal if they are not identical.
18
self.assertNotEqual(TestCase(), TestCase())
36
21
class TestAssertions(TestCase):
228
213
self.test = TestAddCleanup.LoggingTest('runTest')
229
214
self.logging_result = LoggingResult(self._result_calls)
231
def assertLogEqual(self, messages):
216
def assertErrorLogEqual(self, messages):
217
self.assertEqual(messages, [call[0] for call in self._result_calls])
219
def assertTestLogEqual(self, messages):
232
220
"""Assert that the call log equals `messages`."""
233
221
self.assertEqual(messages, self.test._calls)
247
235
# This test doesn't test addCleanup itself, it just sanity checks the
249
237
self.test.run(self.logging_result)
250
self.assertLogEqual(['setUp', 'runTest', 'tearDown'])
238
self.assertTestLogEqual(['setUp', 'runTest', 'tearDown'])
252
240
def test_cleanup_run_before_tearDown(self):
253
241
# Cleanup functions added with 'addCleanup' are called before tearDown
255
243
self.test.addCleanup(self.logAppender, 'cleanup')
256
244
self.test.run(self.logging_result)
257
self.assertLogEqual(['setUp', 'runTest', 'cleanup', 'tearDown'])
245
self.assertTestLogEqual(['setUp', 'runTest', 'cleanup', 'tearDown'])
259
247
def test_add_cleanup_called_if_setUp_fails(self):
260
248
# Cleanup functions added with 'addCleanup' are called even if setUp
263
251
self.test.setUp = self.test.brokenSetUp
264
252
self.test.addCleanup(self.logAppender, 'cleanup')
265
253
self.test.run(self.logging_result)
266
self.assertLogEqual(['brokenSetUp', 'cleanup'])
254
self.assertTestLogEqual(['brokenSetUp', 'cleanup'])
268
256
def test_addCleanup_called_in_reverse_order(self):
269
257
# Cleanup functions added with 'addCleanup' are called in reverse
280
268
self.test.addCleanup(self.logAppender, 'first')
281
269
self.test.addCleanup(self.logAppender, 'second')
282
270
self.test.run(self.logging_result)
271
self.assertTestLogEqual(
284
272
['setUp', 'runTest', 'second', 'first', 'tearDown'])
286
274
def test_tearDown_runs_after_cleanup_failure(self):
287
275
# tearDown runs even if a cleanup function fails.
288
276
self.test.addCleanup(lambda: 1/0)
289
277
self.test.run(self.logging_result)
290
self.assertLogEqual(['setUp', 'runTest', 'tearDown'])
278
self.assertTestLogEqual(['setUp', 'runTest', 'tearDown'])
292
280
def test_cleanups_continue_running_after_error(self):
293
281
# All cleanups are always run, even if one or two of them fail.
295
283
self.test.addCleanup(lambda: 1/0)
296
284
self.test.addCleanup(self.logAppender, 'second')
297
285
self.test.run(self.logging_result)
286
self.assertTestLogEqual(
299
287
['setUp', 'runTest', 'second', 'first', 'tearDown'])
301
289
def test_error_in_cleanups_are_captured(self):
303
291
# test, even though we go on to run other cleanups.
304
292
self.test.addCleanup(lambda: 1/0)
305
293
self.test.run(self.logging_result)
307
['startTest', 'addError', 'stopTest'], self._result_calls)
294
self.assertErrorLogEqual(['startTest', 'addError', 'stopTest'])
309
296
def test_keyboard_interrupt_not_caught(self):
310
297
# If a cleanup raises KeyboardInterrupt, it gets reraised.
319
306
self.test.addCleanup(lambda: 1/0)
320
307
self.test.addCleanup(lambda: 1/0)
321
308
self.test.run(self.logging_result)
323
['startTest', 'addError', 'addError', 'stopTest'],
309
self.assertErrorLogEqual(
310
['startTest', 'addError', 'addError', 'stopTest'])
327
313
class TestUniqueFactories(TestCase):