~cbrandily/python-fixtures/cleanup-context

« back to all changes in this revision

Viewing changes to fixtures/tests/test_fixture.py

  • Committer: Cedric Brandily
  • Date: 2015-05-21 05:16:24 UTC
  • Revision ID: zzelle@gmail.com-20150521051624-lonjt6m2x23faxdy
Ensure cleanups are called even if setUp fails with fixture context

This change ensures that a fixture cleanups will be called even if
its setUp fails when used as a context.


Example:

  with MyFixture() as fixture1:
    pass

fixture1.cleanUp will be called even if fixture1.setUp (called in
fixture1.__enter__) fails.

Show diffs side-by-side

added added

removed removed

Lines of Context:
311
311
        self.assertEqual(126, obj.value)
312
312
        fixture.cleanUp()
313
313
        self.assertEqual(84, obj.value)
 
314
 
 
315
    def test_context_cleanUp(self):
 
316
        class SimpleFixture(fixtures.Fixture):
 
317
            def __init__(self):
 
318
                self.cleanup_called = False
 
319
            def cleanUp(self):
 
320
                self.cleanup_called = True
 
321
 
 
322
        fixture = SimpleFixture()
 
323
        self.assertFalse(fixture.cleanup_called)
 
324
        with fixture:
 
325
            pass
 
326
        self.assertTrue(fixture.cleanup_called)
 
327
 
 
328
    def test_context_cleanUp_with_failed_suite(self):
 
329
        class SimpleFixture(fixtures.Fixture):
 
330
            def __init__(self):
 
331
                self.cleanup_called = False
 
332
            def cleanUp(self):
 
333
                self.cleanup_called = True
 
334
 
 
335
        fixture = SimpleFixture()
 
336
        def func():
 
337
            with fixture:
 
338
                raise ValueError()
 
339
 
 
340
        self.assertFalse(fixture.cleanup_called)
 
341
        self.assertRaises(ValueError, func)
 
342
        self.assertTrue(fixture.cleanup_called)
 
343
 
 
344
    def test_context_cleanUp_with_failed_setUp(self):
 
345
        class FailedSetUpFixture(fixtures.Fixture):
 
346
            def __init__(self):
 
347
                self.cleanup_called = False
 
348
            def setUp(self):
 
349
                super(FailedSetUpFixture, self).setUp()
 
350
                raise ValueError()
 
351
            def cleanUp(self):
 
352
                self.cleanup_called = True
 
353
 
 
354
        fixture = FailedSetUpFixture()
 
355
        def func():
 
356
            with fixture:
 
357
                pass
 
358
 
 
359
        self.assertFalse(fixture.cleanup_called)
 
360
        self.assertRaises(ValueError, func)
 
361
        self.assertTrue(fixture.cleanup_called)