~bzr/ubuntu/natty/python-testtools/bzr-ppa

« back to all changes in this revision

Viewing changes to pyunit3k/tests/test_pyunit3k.py

  • Committer: Jonathan Lange
  • Date: 2008-07-23 01:39:20 UTC
  • mfrom: (6.2.5 result-tests)
  • Revision ID: jml@canonical.com-20080723013920-hz0fdus6fe12eqw9
Tests for pyunit3k's TestResult extensions.

 * Author: jml
 * Reviewer: spiv

Add tests for TestResult and MultiTestResult. As yet no tests for the base
functionality of TestResult.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
"""Tests for extensions to the base test library."""
4
4
 
5
 
from pyunit3k import TestCase, TestResult, clone_test_with_new_id
6
 
 
7
 
 
8
 
class LoggingResult(TestResult):
9
 
    """TestResult that logs its event to a list."""
10
 
 
11
 
    def __init__(self, log):
12
 
        self._events = log
13
 
        super(LoggingResult, self).__init__()
14
 
 
15
 
    def startTest(self, test):
16
 
        self._events.append('startTest')
17
 
        super(LoggingResult, self).startTest(test)
18
 
 
19
 
    def stopTest(self, test):
20
 
        self._events.append('stopTest')
21
 
        super(LoggingResult, self).stopTest(test)
22
 
 
23
 
    def addFailure(self, *args):
24
 
        self._events.append('addFailure')
25
 
        super(LoggingResult, self).addFailure(*args)
26
 
 
27
 
    def addError(self, *args):
28
 
        self._events.append('addError')
29
 
        super(LoggingResult, self).addError(*args)
30
 
 
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
 
7
 
 
8
 
 
9
class TestEquality(TestCase):
 
10
    """Test `TestCase`'s equality implementation."""
 
11
 
 
12
    def test_identicalIsEqual(self):
 
13
        # TestCase's are equal if they are identical.
 
14
        self.assertEqual(self, self)
 
15
 
 
16
    def test_nonIdenticalInUnequal(self):
 
17
        # TestCase's are not equal if they are not identical.
 
18
        self.assertNotEqual(TestCase(), TestCase())
34
19
 
35
20
 
36
21
class TestAssertions(TestCase):
228
213
        self.test = TestAddCleanup.LoggingTest('runTest')
229
214
        self.logging_result = LoggingResult(self._result_calls)
230
215
 
231
 
    def assertLogEqual(self, messages):
 
216
    def assertErrorLogEqual(self, messages):
 
217
        self.assertEqual(messages, [call[0] for call in self._result_calls])
 
218
 
 
219
    def assertTestLogEqual(self, messages):
232
220
        """Assert that the call log equals `messages`."""
233
221
        self.assertEqual(messages, self.test._calls)
234
222
 
247
235
        # This test doesn't test addCleanup itself, it just sanity checks the
248
236
        # fixture.
249
237
        self.test.run(self.logging_result)
250
 
        self.assertLogEqual(['setUp', 'runTest', 'tearDown'])
 
238
        self.assertTestLogEqual(['setUp', 'runTest', 'tearDown'])
251
239
 
252
240
    def test_cleanup_run_before_tearDown(self):
253
241
        # Cleanup functions added with 'addCleanup' are called before tearDown
254
242
        # runs.
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'])
258
246
 
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'])
267
255
 
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)
283
 
        self.assertLogEqual(
 
271
        self.assertTestLogEqual(
284
272
            ['setUp', 'runTest', 'second', 'first', 'tearDown'])
285
273
 
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'])
291
279
 
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)
298
 
        self.assertLogEqual(
 
286
        self.assertTestLogEqual(
299
287
            ['setUp', 'runTest', 'second', 'first', 'tearDown'])
300
288
 
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)
306
 
        self.assertEqual(
307
 
            ['startTest', 'addError', 'stopTest'], self._result_calls)
 
294
        self.assertErrorLogEqual(['startTest', 'addError', 'stopTest'])
308
295
 
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)
322
 
        self.assertEqual(
323
 
            ['startTest', 'addError', 'addError', 'stopTest'],
324
 
            self._result_calls)
 
309
        self.assertErrorLogEqual(
 
310
            ['startTest', 'addError', 'addError', 'stopTest'])
325
311
 
326
312
 
327
313
class TestUniqueFactories(TestCase):