@@ -106,11 +106,13 @@ def testCleanupInRun(self):
106106 class TestableTest (unittest .TestCase ):
107107 def setUp (self ):
108108 ordering .append ('setUp' )
109+ test .addCleanup (cleanup2 )
109110 if blowUp :
110111 raise Exception ('foo' )
111112
112113 def testNothing (self ):
113114 ordering .append ('test' )
115+ test .addCleanup (cleanup3 )
114116
115117 def tearDown (self ):
116118 ordering .append ('tearDown' )
@@ -121,8 +123,9 @@ def cleanup1():
121123 ordering .append ('cleanup1' )
122124 def cleanup2 ():
123125 ordering .append ('cleanup2' )
126+ def cleanup3 ():
127+ ordering .append ('cleanup3' )
124128 test .addCleanup (cleanup1 )
125- test .addCleanup (cleanup2 )
126129
127130 def success (some_test ):
128131 self .assertEqual (some_test , test )
@@ -132,15 +135,15 @@ def success(some_test):
132135 result .addSuccess = success
133136
134137 test .run (result )
135- self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' ,
138+ self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' , 'cleanup3' ,
136139 'cleanup2' , 'cleanup1' , 'success' ])
137140
138141 blowUp = True
139142 ordering = []
140143 test = TestableTest ('testNothing' )
141144 test .addCleanup (cleanup1 )
142145 test .run (result )
143- self .assertEqual (ordering , ['setUp' , 'cleanup1' ])
146+ self .assertEqual (ordering , ['setUp' , 'cleanup2' , ' cleanup1' ])
144147
145148 def testTestCaseDebugExecutesCleanups (self ):
146149 ordering = []
@@ -152,9 +155,11 @@ def setUp(self):
152155
153156 def testNothing (self ):
154157 ordering .append ('test' )
158+ self .addCleanup (cleanup3 )
155159
156160 def tearDown (self ):
157161 ordering .append ('tearDown' )
162+ test .addCleanup (cleanup4 )
158163
159164 test = TestableTest ('testNothing' )
160165
@@ -163,9 +168,14 @@ def cleanup1():
163168 test .addCleanup (cleanup2 )
164169 def cleanup2 ():
165170 ordering .append ('cleanup2' )
171+ def cleanup3 ():
172+ ordering .append ('cleanup3' )
173+ def cleanup4 ():
174+ ordering .append ('cleanup4' )
166175
167176 test .debug ()
168- self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' , 'cleanup1' , 'cleanup2' ])
177+ self .assertEqual (ordering , ['setUp' , 'test' , 'tearDown' , 'cleanup4' ,
178+ 'cleanup3' , 'cleanup1' , 'cleanup2' ])
169179
170180
171181class TestClassCleanup (unittest .TestCase ):
@@ -291,13 +301,14 @@ def testNothing(self):
291301 ordering .append ('test' )
292302 @classmethod
293303 def tearDownClass (cls ):
304+ ordering .append ('tearDownClass' )
294305 raise Exception ('TearDownClassExc' )
295306
296307 suite = unittest .defaultTestLoader .loadTestsFromTestCase (TestableTest )
297308 with self .assertRaises (Exception ) as cm :
298309 suite .debug ()
299310 self .assertEqual (str (cm .exception ), 'TearDownClassExc' )
300- self .assertEqual (ordering , ['setUpClass' , 'test' ])
311+ self .assertEqual (ordering , ['setUpClass' , 'test' , 'tearDownClass' ])
301312 self .assertTrue (TestableTest ._class_cleanups )
302313 TestableTest ._class_cleanups .clear ()
303314
@@ -307,7 +318,7 @@ def tearDownClass(cls):
307318 with self .assertRaises (Exception ) as cm :
308319 suite .debug ()
309320 self .assertEqual (str (cm .exception ), 'TearDownClassExc' )
310- self .assertEqual (ordering , ['setUpClass' , 'test' ])
321+ self .assertEqual (ordering , ['setUpClass' , 'test' , 'tearDownClass' ])
311322 self .assertTrue (TestableTest ._class_cleanups )
312323 TestableTest ._class_cleanups .clear ()
313324
@@ -657,6 +668,7 @@ def setUpModule():
657668 unittest .addModuleCleanup (cleanup , ordering )
658669 @staticmethod
659670 def tearDownModule ():
671+ ordering .append ('tearDownModule' )
660672 raise Exception ('CleanUpExc' )
661673
662674 class TestableTest (unittest .TestCase ):
@@ -675,7 +687,8 @@ def tearDownClass(cls):
675687 self .assertEqual (result .errors [0 ][1 ].splitlines ()[- 1 ],
676688 'Exception: CleanUpExc' )
677689 self .assertEqual (ordering , ['setUpModule' , 'setUpClass' , 'test' ,
678- 'tearDownClass' , 'cleanup_good' ])
690+ 'tearDownClass' , 'tearDownModule' ,
691+ 'cleanup_good' ])
679692 self .assertEqual (unittest .case ._module_cleanups , [])
680693
681694 def test_debug_module_executes_cleanUp (self ):
@@ -729,6 +742,7 @@ def setUpModule():
729742 unittest .addModuleCleanup (cleanup , ordering , blowUp = blowUp )
730743 @staticmethod
731744 def tearDownModule ():
745+ ordering .append ('tearDownModule' )
732746 raise Exception ('TearDownModuleExc' )
733747
734748 class TestableTest (unittest .TestCase ):
@@ -748,7 +762,7 @@ def tearDownClass(cls):
748762 suite .debug ()
749763 self .assertEqual (str (cm .exception ), 'TearDownModuleExc' )
750764 self .assertEqual (ordering , ['setUpModule' , 'setUpClass' , 'test' ,
751- 'tearDownClass' ])
765+ 'tearDownClass' , 'tearDownModule' ])
752766 self .assertTrue (unittest .case ._module_cleanups )
753767 unittest .case ._module_cleanups .clear ()
754768
@@ -759,7 +773,7 @@ def tearDownClass(cls):
759773 suite .debug ()
760774 self .assertEqual (str (cm .exception ), 'TearDownModuleExc' )
761775 self .assertEqual (ordering , ['setUpModule' , 'setUpClass' , 'test' ,
762- 'tearDownClass' ])
776+ 'tearDownClass' , 'tearDownModule' ])
763777 self .assertTrue (unittest .case ._module_cleanups )
764778 unittest .case ._module_cleanups .clear ()
765779
0 commit comments