@@ -134,11 +134,13 @@ def testCleanupInRun(self):
134134 class TestableTest (unittest .TestCase ):
135135 def setUp (self ):
136136 ordering .append ('setUp' )
137+ test .addCleanup (cleanup2 )
137138 if blowUp :
138139 raise Exception ('foo' )
139140
140141 def testNothing (self ):
141142 ordering .append ('test' )
143+ test .addCleanup (cleanup3 )
142144
143145 def tearDown (self ):
144146 ordering .append ('tearDown' )
@@ -149,8 +151,9 @@ def cleanup1():
149151 ordering .append ('cleanup1' )
150152 def cleanup2 ():
151153 ordering .append ('cleanup2' )
154+ def cleanup3 ():
155+ ordering .append ('cleanup3' )
152156 test .addCleanup (cleanup1 )
153- test .addCleanup (cleanup2 )
154157
155158 def success (some_test ):
156159 self .assertEqual (some_test , test )
@@ -160,15 +163,15 @@ def success(some_test):
160163 result .addSuccess = success
161164
162165 test .run (result )
163- self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' ,
166+ self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' , 'cleanup3' ,
164167 'cleanup2' , 'cleanup1' , 'success' ])
165168
166169 blowUp = True
167170 ordering = []
168171 test = TestableTest ('testNothing' )
169172 test .addCleanup (cleanup1 )
170173 test .run (result )
171- self .assertEqual (ordering , ['setUp' , 'cleanup1' ])
174+ self .assertEqual (ordering , ['setUp' , 'cleanup2' , ' cleanup1' ])
172175
173176 def testTestCaseDebugExecutesCleanups (self ):
174177 ordering = []
@@ -180,9 +183,11 @@ def setUp(self):
180183
181184 def testNothing (self ):
182185 ordering .append ('test' )
186+ self .addCleanup (cleanup3 )
183187
184188 def tearDown (self ):
185189 ordering .append ('tearDown' )
190+ test .addCleanup (cleanup4 )
186191
187192 test = TestableTest ('testNothing' )
188193
@@ -191,9 +196,14 @@ def cleanup1():
191196 test .addCleanup (cleanup2 )
192197 def cleanup2 ():
193198 ordering .append ('cleanup2' )
199+ def cleanup3 ():
200+ ordering .append ('cleanup3' )
201+ def cleanup4 ():
202+ ordering .append ('cleanup4' )
194203
195204 test .debug ()
196- self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' , 'cleanup1' , 'cleanup2' ])
205+ self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' , 'cleanup4' ,
206+ 'cleanup3' , 'cleanup1' , 'cleanup2' ])
197207
198208
199209 def test_enterContext (self ):
@@ -352,13 +362,14 @@ def testNothing(self):
352362 ordering .append ('test' )
353363 @classmethod
354364 def tearDownClass (cls ):
365+ ordering .append ('tearDownClass' )
355366 raise Exception ('TearDownClassExc' )
356367
357368 suite = unittest .defaultTestLoader .loadTestsFromTestCase (TestableTest )
358369 with self .assertRaises (Exception ) as cm :
359370 suite .debug ()
360371 self .assertEqual (str (cm .exception ), 'TearDownClassExc' )
361- self .assertEqual (ordering , ['setUpClass' , 'test' ])
372+ self .assertEqual (ordering , ['setUpClass' , 'test' , 'tearDownClass' ])
362373 self .assertTrue (TestableTest ._class_cleanups )
363374 TestableTest ._class_cleanups .clear ()
364375
@@ -368,7 +379,7 @@ def tearDownClass(cls):
368379 with self .assertRaises (Exception ) as cm :
369380 suite .debug ()
370381 self .assertEqual (str (cm .exception ), 'TearDownClassExc' )
371- self .assertEqual (ordering , ['setUpClass' , 'test' ])
382+ self .assertEqual (ordering , ['setUpClass' , 'test' , 'tearDownClass' ])
372383 self .assertTrue (TestableTest ._class_cleanups )
373384 TestableTest ._class_cleanups .clear ()
374385
@@ -747,6 +758,7 @@ def setUpModule():
747758 unittest .addModuleCleanup (cleanup , ordering )
748759 @staticmethod
749760 def tearDownModule ():
761+ ordering .append ('tearDownModule' )
750762 raise Exception ('CleanUpExc' )
751763
752764 class TestableTest (unittest .TestCase ):
@@ -765,7 +777,8 @@ def tearDownClass(cls):
765777 self .assertEqual (result .errors [0 ][1 ].splitlines ()[- 1 ],
766778 'Exception: CleanUpExc' )
767779 self .assertEqual (ordering , ['setUpModule' , 'setUpClass' , 'test' ,
768- 'tearDownClass' , 'cleanup_good' ])
780+ 'tearDownClass' , 'tearDownModule' ,
781+ 'cleanup_good' ])
769782 self .assertEqual (unittest .case ._module_cleanups , [])
770783
771784 def test_debug_module_executes_cleanUp (self ):
@@ -819,6 +832,7 @@ def setUpModule():
819832 unittest .addModuleCleanup (cleanup , ordering , blowUp = blowUp )
820833 @staticmethod
821834 def tearDownModule ():
835+ ordering .append ('tearDownModule' )
822836 raise Exception ('TearDownModuleExc' )
823837
824838 class TestableTest (unittest .TestCase ):
@@ -838,7 +852,7 @@ def tearDownClass(cls):
838852 suite .debug ()
839853 self .assertEqual (str (cm .exception ), 'TearDownModuleExc' )
840854 self .assertEqual (ordering , ['setUpModule' , 'setUpClass' , 'test' ,
841- 'tearDownClass' ])
855+ 'tearDownClass' , 'tearDownModule' ])
842856 self .assertTrue (unittest .case ._module_cleanups )
843857 unittest .case ._module_cleanups .clear ()
844858
@@ -849,7 +863,7 @@ def tearDownClass(cls):
849863 suite .debug ()
850864 self .assertEqual (str (cm .exception ), 'TearDownModuleExc' )
851865 self .assertEqual (ordering , ['setUpModule' , 'setUpClass' , 'test' ,
852- 'tearDownClass' ])
866+ 'tearDownClass' , 'tearDownModule' ])
853867 self .assertTrue (unittest .case ._module_cleanups )
854868 unittest .case ._module_cleanups .clear ()
855869
0 commit comments