@@ -106,11 +106,13 @@ def testCleanupInRun(self):
106
106
class TestableTest (unittest .TestCase ):
107
107
def setUp (self ):
108
108
ordering .append ('setUp' )
109
+ test .addCleanup (cleanup2 )
109
110
if blowUp :
110
111
raise Exception ('foo' )
111
112
112
113
def testNothing (self ):
113
114
ordering .append ('test' )
115
+ test .addCleanup (cleanup3 )
114
116
115
117
def tearDown (self ):
116
118
ordering .append ('tearDown' )
@@ -121,8 +123,9 @@ def cleanup1():
121
123
ordering .append ('cleanup1' )
122
124
def cleanup2 ():
123
125
ordering .append ('cleanup2' )
126
+ def cleanup3 ():
127
+ ordering .append ('cleanup3' )
124
128
test .addCleanup (cleanup1 )
125
- test .addCleanup (cleanup2 )
126
129
127
130
def success (some_test ):
128
131
self .assertEqual (some_test , test )
@@ -132,15 +135,15 @@ def success(some_test):
132
135
result .addSuccess = success
133
136
134
137
test .run (result )
135
- self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' ,
138
+ self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' , 'cleanup3' ,
136
139
'cleanup2' , 'cleanup1' , 'success' ])
137
140
138
141
blowUp = True
139
142
ordering = []
140
143
test = TestableTest ('testNothing' )
141
144
test .addCleanup (cleanup1 )
142
145
test .run (result )
143
- self .assertEqual (ordering , ['setUp' , 'cleanup1' ])
146
+ self .assertEqual (ordering , ['setUp' , 'cleanup2' , ' cleanup1' ])
144
147
145
148
def testTestCaseDebugExecutesCleanups (self ):
146
149
ordering = []
@@ -152,9 +155,11 @@ def setUp(self):
152
155
153
156
def testNothing (self ):
154
157
ordering .append ('test' )
158
+ self .addCleanup (cleanup3 )
155
159
156
160
def tearDown (self ):
157
161
ordering .append ('tearDown' )
162
+ test .addCleanup (cleanup4 )
158
163
159
164
test = TestableTest ('testNothing' )
160
165
@@ -163,9 +168,14 @@ def cleanup1():
163
168
test .addCleanup (cleanup2 )
164
169
def cleanup2 ():
165
170
ordering .append ('cleanup2' )
171
+ def cleanup3 ():
172
+ ordering .append ('cleanup3' )
173
+ def cleanup4 ():
174
+ ordering .append ('cleanup4' )
166
175
167
176
test .debug ()
168
- self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' , 'cleanup1' , 'cleanup2' ])
177
+ self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' , 'cleanup4' ,
178
+ 'cleanup3' , 'cleanup1' , 'cleanup2' ])
169
179
170
180
171
181
class TestClassCleanup (unittest .TestCase ):
@@ -291,13 +301,14 @@ def testNothing(self):
291
301
ordering .append ('test' )
292
302
@classmethod
293
303
def tearDownClass (cls ):
304
+ ordering .append ('tearDownClass' )
294
305
raise Exception ('TearDownClassExc' )
295
306
296
307
suite = unittest .defaultTestLoader .loadTestsFromTestCase (TestableTest )
297
308
with self .assertRaises (Exception ) as cm :
298
309
suite .debug ()
299
310
self .assertEqual (str (cm .exception ), 'TearDownClassExc' )
300
- self .assertEqual (ordering , ['setUpClass' , 'test' ])
311
+ self .assertEqual (ordering , ['setUpClass' , 'test' , 'tearDownClass' ])
301
312
self .assertTrue (TestableTest ._class_cleanups )
302
313
TestableTest ._class_cleanups .clear ()
303
314
@@ -307,7 +318,7 @@ def tearDownClass(cls):
307
318
with self .assertRaises (Exception ) as cm :
308
319
suite .debug ()
309
320
self .assertEqual (str (cm .exception ), 'TearDownClassExc' )
310
- self .assertEqual (ordering , ['setUpClass' , 'test' ])
321
+ self .assertEqual (ordering , ['setUpClass' , 'test' , 'tearDownClass' ])
311
322
self .assertTrue (TestableTest ._class_cleanups )
312
323
TestableTest ._class_cleanups .clear ()
313
324
@@ -657,6 +668,7 @@ def setUpModule():
657
668
unittest .addModuleCleanup (cleanup , ordering )
658
669
@staticmethod
659
670
def tearDownModule ():
671
+ ordering .append ('tearDownModule' )
660
672
raise Exception ('CleanUpExc' )
661
673
662
674
class TestableTest (unittest .TestCase ):
@@ -675,7 +687,8 @@ def tearDownClass(cls):
675
687
self .assertEqual (result .errors [0 ][1 ].splitlines ()[- 1 ],
676
688
'Exception: CleanUpExc' )
677
689
self .assertEqual (ordering , ['setUpModule' , 'setUpClass' , 'test' ,
678
- 'tearDownClass' , 'cleanup_good' ])
690
+ 'tearDownClass' , 'tearDownModule' ,
691
+ 'cleanup_good' ])
679
692
self .assertEqual (unittest .case ._module_cleanups , [])
680
693
681
694
def test_debug_module_executes_cleanUp (self ):
@@ -729,6 +742,7 @@ def setUpModule():
729
742
unittest .addModuleCleanup (cleanup , ordering , blowUp = blowUp )
730
743
@staticmethod
731
744
def tearDownModule ():
745
+ ordering .append ('tearDownModule' )
732
746
raise Exception ('TearDownModuleExc' )
733
747
734
748
class TestableTest (unittest .TestCase ):
@@ -748,7 +762,7 @@ def tearDownClass(cls):
748
762
suite .debug ()
749
763
self .assertEqual (str (cm .exception ), 'TearDownModuleExc' )
750
764
self .assertEqual (ordering , ['setUpModule' , 'setUpClass' , 'test' ,
751
- 'tearDownClass' ])
765
+ 'tearDownClass' , 'tearDownModule' ])
752
766
self .assertTrue (unittest .case ._module_cleanups )
753
767
unittest .case ._module_cleanups .clear ()
754
768
@@ -759,7 +773,7 @@ def tearDownClass(cls):
759
773
suite .debug ()
760
774
self .assertEqual (str (cm .exception ), 'TearDownModuleExc' )
761
775
self .assertEqual (ordering , ['setUpModule' , 'setUpClass' , 'test' ,
762
- 'tearDownClass' ])
776
+ 'tearDownClass' , 'tearDownModule' ])
763
777
self .assertTrue (unittest .case ._module_cleanups )
764
778
unittest .case ._module_cleanups .clear ()
765
779
0 commit comments