@@ -134,11 +134,13 @@ def testCleanupInRun(self):
134
134
class TestableTest (unittest .TestCase ):
135
135
def setUp (self ):
136
136
ordering .append ('setUp' )
137
+ test .addCleanup (cleanup2 )
137
138
if blowUp :
138
139
raise Exception ('foo' )
139
140
140
141
def testNothing (self ):
141
142
ordering .append ('test' )
143
+ test .addCleanup (cleanup3 )
142
144
143
145
def tearDown (self ):
144
146
ordering .append ('tearDown' )
@@ -149,8 +151,9 @@ def cleanup1():
149
151
ordering .append ('cleanup1' )
150
152
def cleanup2 ():
151
153
ordering .append ('cleanup2' )
154
+ def cleanup3 ():
155
+ ordering .append ('cleanup3' )
152
156
test .addCleanup (cleanup1 )
153
- test .addCleanup (cleanup2 )
154
157
155
158
def success (some_test ):
156
159
self .assertEqual (some_test , test )
@@ -160,15 +163,15 @@ def success(some_test):
160
163
result .addSuccess = success
161
164
162
165
test .run (result )
163
- self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' ,
166
+ self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' , 'cleanup3' ,
164
167
'cleanup2' , 'cleanup1' , 'success' ])
165
168
166
169
blowUp = True
167
170
ordering = []
168
171
test = TestableTest ('testNothing' )
169
172
test .addCleanup (cleanup1 )
170
173
test .run (result )
171
- self .assertEqual (ordering , ['setUp' , 'cleanup1' ])
174
+ self .assertEqual (ordering , ['setUp' , 'cleanup2' , ' cleanup1' ])
172
175
173
176
def testTestCaseDebugExecutesCleanups (self ):
174
177
ordering = []
@@ -180,9 +183,11 @@ def setUp(self):
180
183
181
184
def testNothing (self ):
182
185
ordering .append ('test' )
186
+ self .addCleanup (cleanup3 )
183
187
184
188
def tearDown (self ):
185
189
ordering .append ('tearDown' )
190
+ test .addCleanup (cleanup4 )
186
191
187
192
test = TestableTest ('testNothing' )
188
193
@@ -191,9 +196,14 @@ def cleanup1():
191
196
test .addCleanup (cleanup2 )
192
197
def cleanup2 ():
193
198
ordering .append ('cleanup2' )
199
+ def cleanup3 ():
200
+ ordering .append ('cleanup3' )
201
+ def cleanup4 ():
202
+ ordering .append ('cleanup4' )
194
203
195
204
test .debug ()
196
- self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' , 'cleanup1' , 'cleanup2' ])
205
+ self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' , 'cleanup4' ,
206
+ 'cleanup3' , 'cleanup1' , 'cleanup2' ])
197
207
198
208
199
209
def test_enterContext (self ):
@@ -352,13 +362,14 @@ def testNothing(self):
352
362
ordering .append ('test' )
353
363
@classmethod
354
364
def tearDownClass (cls ):
365
+ ordering .append ('tearDownClass' )
355
366
raise Exception ('TearDownClassExc' )
356
367
357
368
suite = unittest .defaultTestLoader .loadTestsFromTestCase (TestableTest )
358
369
with self .assertRaises (Exception ) as cm :
359
370
suite .debug ()
360
371
self .assertEqual (str (cm .exception ), 'TearDownClassExc' )
361
- self .assertEqual (ordering , ['setUpClass' , 'test' ])
372
+ self .assertEqual (ordering , ['setUpClass' , 'test' , 'tearDownClass' ])
362
373
self .assertTrue (TestableTest ._class_cleanups )
363
374
TestableTest ._class_cleanups .clear ()
364
375
@@ -368,7 +379,7 @@ def tearDownClass(cls):
368
379
with self .assertRaises (Exception ) as cm :
369
380
suite .debug ()
370
381
self .assertEqual (str (cm .exception ), 'TearDownClassExc' )
371
- self .assertEqual (ordering , ['setUpClass' , 'test' ])
382
+ self .assertEqual (ordering , ['setUpClass' , 'test' , 'tearDownClass' ])
372
383
self .assertTrue (TestableTest ._class_cleanups )
373
384
TestableTest ._class_cleanups .clear ()
374
385
@@ -747,6 +758,7 @@ def setUpModule():
747
758
unittest .addModuleCleanup (cleanup , ordering )
748
759
@staticmethod
749
760
def tearDownModule ():
761
+ ordering .append ('tearDownModule' )
750
762
raise Exception ('CleanUpExc' )
751
763
752
764
class TestableTest (unittest .TestCase ):
@@ -765,7 +777,8 @@ def tearDownClass(cls):
765
777
self .assertEqual (result .errors [0 ][1 ].splitlines ()[- 1 ],
766
778
'Exception: CleanUpExc' )
767
779
self .assertEqual (ordering , ['setUpModule' , 'setUpClass' , 'test' ,
768
- 'tearDownClass' , 'cleanup_good' ])
780
+ 'tearDownClass' , 'tearDownModule' ,
781
+ 'cleanup_good' ])
769
782
self .assertEqual (unittest .case ._module_cleanups , [])
770
783
771
784
def test_debug_module_executes_cleanUp (self ):
@@ -819,6 +832,7 @@ def setUpModule():
819
832
unittest .addModuleCleanup (cleanup , ordering , blowUp = blowUp )
820
833
@staticmethod
821
834
def tearDownModule ():
835
+ ordering .append ('tearDownModule' )
822
836
raise Exception ('TearDownModuleExc' )
823
837
824
838
class TestableTest (unittest .TestCase ):
@@ -838,7 +852,7 @@ def tearDownClass(cls):
838
852
suite .debug ()
839
853
self .assertEqual (str (cm .exception ), 'TearDownModuleExc' )
840
854
self .assertEqual (ordering , ['setUpModule' , 'setUpClass' , 'test' ,
841
- 'tearDownClass' ])
855
+ 'tearDownClass' , 'tearDownModule' ])
842
856
self .assertTrue (unittest .case ._module_cleanups )
843
857
unittest .case ._module_cleanups .clear ()
844
858
@@ -849,7 +863,7 @@ def tearDownClass(cls):
849
863
suite .debug ()
850
864
self .assertEqual (str (cm .exception ), 'TearDownModuleExc' )
851
865
self .assertEqual (ordering , ['setUpModule' , 'setUpClass' , 'test' ,
852
- 'tearDownClass' ])
866
+ 'tearDownClass' , 'tearDownModule' ])
853
867
self .assertTrue (unittest .case ._module_cleanups )
854
868
unittest .case ._module_cleanups .clear ()
855
869
0 commit comments