~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/testing/tests/test_fixture.py

  • Committer: Robert Collins
  • Date: 2010-10-17 21:21:07 UTC
  • mto: This revision was merged to the branch mainline in revision 11736.
  • Revision ID: robert@canonical.com-20101017212107-gyp05138htnu3aek
Rationalise our fixture test support - delete unused stuff, consolidate on the external fixtures API.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
 
# GNU Affero General Public License version 3 (see the file LICENSE).
3
 
 
4
 
"""Tests for fixture support."""
5
 
 
6
 
__metaclass__ = type
7
 
 
8
 
import unittest
9
 
 
10
 
from zope.interface import implements
11
 
 
12
 
from lp.testing import TestCase
13
 
from lp.testing.fixture import (
14
 
    Fixtures,
15
 
    FixtureWithCleanup,
16
 
    IFixture,
17
 
    run_with_fixture,
18
 
    with_fixture,
19
 
    )
20
 
 
21
 
 
22
 
class LoggingFixture:
23
 
 
24
 
    implements(IFixture)
25
 
 
26
 
    def __init__(self, log):
27
 
        self.log = log
28
 
 
29
 
    def setUp(self):
30
 
        self.log.append('setUp')
31
 
 
32
 
    def tearDown(self):
33
 
        self.log.append('tearDown')
34
 
 
35
 
 
36
 
class TestFixture(TestCase):
37
 
 
38
 
    def test_run_with_fixture(self):
39
 
        # run_with_fixture runs the setUp method of the fixture, the passed
40
 
        # function and then the tearDown method of the fixture.
41
 
        log = []
42
 
        fixture = LoggingFixture(log)
43
 
        run_with_fixture(fixture, log.append, 'hello')
44
 
        self.assertEqual(['setUp', 'hello', 'tearDown'], log)
45
 
 
46
 
    def test_run_tearDown_even_with_exception(self):
47
 
        # run_with_fixture runs the setUp method of the fixture, the passed
48
 
        # function and then the tearDown method of the fixture even if the
49
 
        # function raises an exception.
50
 
        log = []
51
 
        fixture = LoggingFixture(log)
52
 
        self.assertRaises(
53
 
            ZeroDivisionError, run_with_fixture, fixture, lambda: 1/0)
54
 
        self.assertEqual(['setUp', 'tearDown'], log)
55
 
 
56
 
    def test_with_fixture(self):
57
 
        # with_fixture decorates a function so that it gets passed the fixture
58
 
        # and the fixture is set up and torn down around the function.
59
 
        log = []
60
 
        fixture = LoggingFixture(log)
61
 
        @with_fixture(fixture)
62
 
        def function(fixture, **kwargs):
63
 
            log.append(fixture)
64
 
            log.append(kwargs)
65
 
            return 'oi'
66
 
        result = function(foo='bar')
67
 
        self.assertEqual('oi', result)
68
 
        self.assertEqual(['setUp', fixture, {'foo': 'bar'}, 'tearDown'], log)
69
 
 
70
 
 
71
 
class TestFixtureWithCleanup(TestCase):
72
 
    """Tests for `FixtureWithCleanup`."""
73
 
 
74
 
    def test_cleanup_called_during_teardown(self):
75
 
        log = []
76
 
        fixture = FixtureWithCleanup()
77
 
        fixture.setUp()
78
 
        fixture.addCleanup(log.append, 'foo')
79
 
        self.assertEqual([], log)
80
 
        fixture.tearDown()
81
 
        self.assertEqual(['foo'], log)
82
 
 
83
 
    def test_cleanup_called_in_reverse_order(self):
84
 
        log = []
85
 
        fixture = FixtureWithCleanup()
86
 
        fixture.setUp()
87
 
        fixture.addCleanup(log.append, 'foo')
88
 
        fixture.addCleanup(log.append, 'bar')
89
 
        fixture.tearDown()
90
 
        self.assertEqual(['bar', 'foo'], log)
91
 
 
92
 
    def test_cleanup_run_even_in_failure(self):
93
 
        log = []
94
 
        fixture = FixtureWithCleanup()
95
 
        fixture.setUp()
96
 
        fixture.addCleanup(log.append, 'foo')
97
 
        fixture.addCleanup(lambda: 1/0)
98
 
        self.assertRaises(ZeroDivisionError, fixture.tearDown)
99
 
        self.assertEqual(['foo'], log)
100
 
 
101
 
 
102
 
class TestFixtures(TestCase):
103
 
    """Tests the `Fixtures` class, which groups multiple `IFixture`s."""
104
 
 
105
 
    class LoggingFixture:
106
 
 
107
 
        def __init__(self, log):
108
 
            self._log = log
109
 
 
110
 
        def setUp(self):
111
 
            self._log.append((self, 'setUp'))
112
 
 
113
 
        def tearDown(self):
114
 
            self._log.append((self, 'tearDown'))
115
 
 
116
 
    def test_with_single_fixture(self):
117
 
        log = []
118
 
        a = self.LoggingFixture(log)
119
 
        fixtures = Fixtures([a])
120
 
        fixtures.setUp()
121
 
        fixtures.tearDown()
122
 
        self.assertEqual([(a, 'setUp'), (a, 'tearDown')], log)
123
 
 
124
 
    def test_with_multiple_fixtures(self):
125
 
        log = []
126
 
        a = self.LoggingFixture(log)
127
 
        b = self.LoggingFixture(log)
128
 
        fixtures = Fixtures([a, b])
129
 
        fixtures.setUp()
130
 
        fixtures.tearDown()
131
 
        self.assertEqual(
132
 
            [(a, 'setUp'), (b, 'setUp'), (b, 'tearDown'), (a, 'tearDown')],
133
 
            log)
134
 
 
135
 
 
136
 
def test_suite():
137
 
    return unittest.TestLoader().loadTestsFromName(__name__)
138