2
from twisted.trial import unittest, reporter, runner
7
class NumberOfRuns(unittest.TestCase):
13
global _setUpClassRuns
14
self.failUnlessEqual(_setUpClassRuns, 1)
17
global _setUpClassRuns
18
self.failUnlessEqual(_setUpClassRuns, 1)
21
global _setUpClassRuns
22
self.failUnlessEqual(_setUpClassRuns, 1)
24
def tearDownClass(self):
25
global _tearDownClassRuns
26
self.failUnlessEqual(_tearDownClassRuns, 0)
27
_tearDownClassRuns += 1
30
class AttributeSetUp(unittest.TestCase):
35
self.failUnless(hasattr(self, 'x'), "Attribute 'x' not set")
36
self.failUnlessEqual(self.x, 42)
39
self.failUnlessEqual(self.x, 42) # still the same
42
self.failUnlessEqual(self.x, 42) # still the same
45
self.failUnlessEqual(self.x, 42) # still the same
47
def tearDownClass(self):
51
class AttributeManipulation(unittest.TestCase):
65
self.failUnless(self.testsRun > 0)
67
def tearDownClass(self):
68
self.failUnlessEqual(self.testsRun, 3)
71
class AttributeSharing(unittest.TestCase):
72
class AttributeSharer(unittest.TestCase):
74
self.first = 'test1Run'
77
self.failIf(hasattr(self, 'first'))
79
class ClassAttributeSharer(AttributeSharer):
84
self.failUnlessEqual('test1Run', self.first)
87
self.loader = runner.TestLoader()
89
def test_normal(self):
90
result = reporter.TestResult()
91
suite = self.loader.loadClass(AttributeSharing.AttributeSharer)
93
self.failUnlessEqual(result.errors, [])
94
self.failUnlessEqual(result.failures, [])
96
def test_shared(self):
97
result = reporter.TestResult()
98
suite = self.loader.loadClass(AttributeSharing.ClassAttributeSharer)
100
self.failUnlessEqual(result.errors, [])
101
self.failUnlessEqual(len(result.failures), 1) # from test_2
102
self.failUnlessEqual(result.failures[0][0].shortDescription(),
106
class FactoryCounting(unittest.TestCase):
107
class MyTestCase(unittest.TestCase):
109
_tearDownClassRun = 0
110
def setUpClass(self):
111
self.__class__._setUpClassRun += 1
119
def tearDownClass(self):
120
self.__class__._tearDownClassRun += 1
122
class AnotherTestCase(MyTestCase):
124
_tearDownClassRun = 0
125
def setUpClass(self):
126
self.__class__._setUpClassRun += 1
127
raise unittest.SkipTest('reason')
135
def tearDownClass(self):
136
self.__class__._tearDownClassRun += 1
140
self.factory = FactoryCounting.MyTestCase
141
self.subFactory = FactoryCounting.AnotherTestCase
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()
150
def test_createAndRun(self):
151
test = self.factory('test_1')
152
self.failUnlessEqual(test._isFirst(), True)
153
result = reporter.TestResult()
155
self.failUnlessEqual(self.factory._setUpClassRun, 1)
156
self.failUnlessEqual(self.factory._tearDownClassRun, 1)
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()
164
self.failUnlessEqual(self.factory._setUpClassRun, 1)
165
self.failUnlessEqual(self.factory._tearDownClassRun, 0)
167
self.failUnlessEqual(self.factory._setUpClassRun, 1)
168
self.failUnlessEqual(self.factory._tearDownClassRun, 1)
170
def test_runTwice(self):
171
test = self.factory('test_1')
172
result = reporter.TestResult()
174
self.failUnlessEqual(self.factory._setUpClassRun, 1)
175
self.failUnlessEqual(self.factory._tearDownClassRun, 1)
177
self.failUnlessEqual(self.factory._setUpClassRun, 2)
178
self.failUnlessEqual(self.factory._tearDownClassRun, 2)
180
def test_runMultipleCopies(self):
181
tests = map(self.factory, ['test_1', 'test_1'])
182
result = reporter.TestResult()
184
self.failUnlessEqual(self.factory._setUpClassRun, 1)
185
self.failUnlessEqual(self.factory._tearDownClassRun, 0)
187
self.failUnlessEqual(self.factory._setUpClassRun, 1)
188
self.failUnlessEqual(self.factory._tearDownClassRun, 1)
190
def test_skippingSetUpClass(self):
191
tests = map(self.subFactory, ['test_1', 'test_2'])
192
result = reporter.TestResult()
194
self.failUnlessEqual(self.subFactory._setUpClassRun, 1)
195
self.failUnlessEqual(self.subFactory._tearDownClassRun, 0)
197
self.failUnlessEqual(self.subFactory._setUpClassRun, 2)
198
self.failUnlessEqual(self.subFactory._tearDownClassRun, 0)