~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/trial/test/test_class.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-02-22 22:52:47 UTC
  • Revision ID: james.westby@ubuntu.com-20060222225247-0mjb8ij9473m5zse
Tags: 2.2.0-1ubuntu1
Synchronize with Debian unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import sets
 
2
from twisted.trial import unittest, reporter, runner
 
3
 
 
4
_setUpClassRuns = 0
 
5
_tearDownClassRuns = 0
 
6
 
 
7
class NumberOfRuns(unittest.TestCase):
 
8
    def setUpClass(self):
 
9
        global _setUpClassRuns
 
10
        _setUpClassRuns += 1
 
11
    
 
12
    def test_1(self):
 
13
        global _setUpClassRuns
 
14
        self.failUnlessEqual(_setUpClassRuns, 1)
 
15
 
 
16
    def test_2(self):
 
17
        global _setUpClassRuns
 
18
        self.failUnlessEqual(_setUpClassRuns, 1)
 
19
 
 
20
    def test_3(self):
 
21
        global _setUpClassRuns
 
22
        self.failUnlessEqual(_setUpClassRuns, 1)
 
23
 
 
24
    def tearDownClass(self):
 
25
        global _tearDownClassRuns
 
26
        self.failUnlessEqual(_tearDownClassRuns, 0)
 
27
        _tearDownClassRuns += 1
 
28
 
 
29
 
 
30
class AttributeSetUp(unittest.TestCase):
 
31
    def setUpClass(self):
 
32
        self.x = 42
 
33
 
 
34
    def setUp(self):
 
35
        self.failUnless(hasattr(self, 'x'), "Attribute 'x' not set")
 
36
        self.failUnlessEqual(self.x, 42)
 
37
 
 
38
    def test_1(self):
 
39
        self.failUnlessEqual(self.x, 42) # still the same
 
40
 
 
41
    def test_2(self):
 
42
        self.failUnlessEqual(self.x, 42) # still the same
 
43
 
 
44
    def tearDown(self):
 
45
        self.failUnlessEqual(self.x, 42) # still the same
 
46
 
 
47
    def tearDownClass(self):
 
48
        self.x = None
 
49
 
 
50
 
 
51
class AttributeManipulation(unittest.TestCase):
 
52
    def setUpClass(self):
 
53
        self.testsRun = 0
 
54
 
 
55
    def test_1(self):
 
56
        self.testsRun += 1
 
57
 
 
58
    def test_2(self):
 
59
        self.testsRun += 1
 
60
 
 
61
    def test_3(self):
 
62
        self.testsRun += 1
 
63
 
 
64
    def tearDown(self):
 
65
        self.failUnless(self.testsRun > 0)
 
66
 
 
67
    def tearDownClass(self):
 
68
        self.failUnlessEqual(self.testsRun, 3)
 
69
 
 
70
 
 
71
class AttributeSharing(unittest.TestCase):
 
72
    class AttributeSharer(unittest.TestCase):
 
73
        def test_1(self):
 
74
            self.first = 'test1Run'
 
75
 
 
76
        def test_2(self):
 
77
            self.failIf(hasattr(self, 'first'))
 
78
 
 
79
    class ClassAttributeSharer(AttributeSharer):
 
80
        def setUpClass(self):
 
81
            pass
 
82
 
 
83
        def test_3(self):
 
84
            self.failUnlessEqual('test1Run', self.first)
 
85
 
 
86
    def setUp(self):
 
87
        self.loader = runner.TestLoader()
 
88
        
 
89
    def test_normal(self):
 
90
        result = reporter.TestResult()
 
91
        suite = self.loader.loadClass(AttributeSharing.AttributeSharer)
 
92
        suite.run(result)
 
93
        self.failUnlessEqual(result.errors, [])
 
94
        self.failUnlessEqual(result.failures, [])
 
95
 
 
96
    def test_shared(self):
 
97
        result = reporter.TestResult()
 
98
        suite = self.loader.loadClass(AttributeSharing.ClassAttributeSharer)
 
99
        suite.run(result)
 
100
        self.failUnlessEqual(result.errors, [])
 
101
        self.failUnlessEqual(len(result.failures), 1) # from test_2
 
102
        self.failUnlessEqual(result.failures[0][0].shortDescription(),
 
103
                             'test_2')
 
104
        
 
105
 
 
106
class FactoryCounting(unittest.TestCase):
 
107
    class MyTestCase(unittest.TestCase):
 
108
        _setUpClassRun = 0
 
109
        _tearDownClassRun = 0
 
110
        def setUpClass(self):
 
111
            self.__class__._setUpClassRun += 1
 
112
        
 
113
        def test_1(self):
 
114
            pass
 
115
 
 
116
        def test_2(self):
 
117
            pass
 
118
 
 
119
        def tearDownClass(self):
 
120
            self.__class__._tearDownClassRun += 1
 
121
 
 
122
    class AnotherTestCase(MyTestCase):
 
123
        _setUpClassRun = 0
 
124
        _tearDownClassRun = 0
 
125
        def setUpClass(self):
 
126
            self.__class__._setUpClassRun += 1
 
127
            raise unittest.SkipTest('reason')
 
128
 
 
129
        def test_1(self):
 
130
            pass
 
131
 
 
132
        def test_2(self):
 
133
            pass
 
134
 
 
135
        def tearDownClass(self):
 
136
            self.__class__._tearDownClassRun += 1
 
137
            
 
138
    
 
139
    def setUp(self):
 
140
        self.factory = FactoryCounting.MyTestCase
 
141
        self.subFactory = FactoryCounting.AnotherTestCase
 
142
        self._reset()
 
143
 
 
144
    def _reset(self):
 
145
        self.factory._setUpClassRun = self.factory._tearDownClassRun = 0
 
146
        self.subFactory._setUpClassRun = self.subFactory._tearDownClassRun = 0
 
147
        self.factory._instances = sets.Set()
 
148
        self.factory._instancesRun = sets.Set()
 
149
 
 
150
    def test_createAndRun(self):
 
151
        test = self.factory('test_1')
 
152
        self.failUnlessEqual(test._isFirst(), True)
 
153
        result = reporter.TestResult()
 
154
        test(result)
 
155
        self.failUnlessEqual(self.factory._setUpClassRun, 1)
 
156
        self.failUnlessEqual(self.factory._tearDownClassRun, 1)
 
157
 
 
158
    def test_createTwoAndRun(self):
 
159
        tests = map(self.factory, ['test_1', 'test_2'])
 
160
        self.failUnlessEqual(tests[0]._isFirst(), True)
 
161
        self.failUnlessEqual(tests[1]._isFirst(), True)
 
162
        result = reporter.TestResult()
 
163
        tests[0](result)
 
164
        self.failUnlessEqual(self.factory._setUpClassRun, 1)
 
165
        self.failUnlessEqual(self.factory._tearDownClassRun, 0)
 
166
        tests[1](result)
 
167
        self.failUnlessEqual(self.factory._setUpClassRun, 1)
 
168
        self.failUnlessEqual(self.factory._tearDownClassRun, 1)
 
169
        
 
170
    def test_runTwice(self):
 
171
        test = self.factory('test_1')
 
172
        result = reporter.TestResult()
 
173
        test(result)
 
174
        self.failUnlessEqual(self.factory._setUpClassRun, 1)
 
175
        self.failUnlessEqual(self.factory._tearDownClassRun, 1)
 
176
        test(result)
 
177
        self.failUnlessEqual(self.factory._setUpClassRun, 2)
 
178
        self.failUnlessEqual(self.factory._tearDownClassRun, 2)
 
179
        
 
180
    def test_runMultipleCopies(self):
 
181
        tests = map(self.factory, ['test_1', 'test_1'])
 
182
        result = reporter.TestResult()
 
183
        tests[0](result)
 
184
        self.failUnlessEqual(self.factory._setUpClassRun, 1)
 
185
        self.failUnlessEqual(self.factory._tearDownClassRun, 0)
 
186
        tests[1](result)
 
187
        self.failUnlessEqual(self.factory._setUpClassRun, 1)
 
188
        self.failUnlessEqual(self.factory._tearDownClassRun, 1)
 
189
        
 
190
    def test_skippingSetUpClass(self):
 
191
        tests = map(self.subFactory, ['test_1', 'test_2'])
 
192
        result = reporter.TestResult()
 
193
        tests[0](result)
 
194
        self.failUnlessEqual(self.subFactory._setUpClassRun, 1)
 
195
        self.failUnlessEqual(self.subFactory._tearDownClassRun, 0)
 
196
        tests[1](result)
 
197
        self.failUnlessEqual(self.subFactory._setUpClassRun, 2)
 
198
        self.failUnlessEqual(self.subFactory._tearDownClassRun, 0)
 
199