Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Lib/unittest/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@ def _handleModuleTearDown(self, result):
previousModule)
finally:
_call_if_exists(result, '_restoreStdout')
try:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it also be called in this finally block? That's the previous behavior, and I imagine it could be important to ensure it's called even when tearDownModule() raises a BaseException.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. I think this is not need TBH, my understanding it that the finally block will be executed anyways in case of a Exception and, since _call_if_exists doesn't return, the clean up is done in the following steps.

Regarding the BaseException, yes, if tearDownModule raises it no doModuleCleanups is performed.

I think it's a good idea to catch it but for consistency we should change setUpClass as well. What do you think?

    setUpClass = getattr(currentClass, 'setUpClass', None)
    if setUpClass is not None:
        _call_if_exists(result, '_setupStdout')
        try:
            setUpClass()
        except Exception as e:
            if isinstance(result, _DebugResult):
                raise`

case.doModuleCleanups()
except Exception as e:
self._createClassOrModuleLevelException(result, e,
'tearDownModule',
previousModule)
try:
case.doModuleCleanups()
except Exception as e:
self._createClassOrModuleLevelException(result, e,
'doModuleCleanups',
previousModule)

def _tearDownPreviousClass(self, test, result):
previousClass = getattr(result, '_previousTestClass', None)
Expand Down
48 changes: 48 additions & 0 deletions Lib/unittest/test/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,54 @@ class Module(object):
self.assertEqual(cleanups,
[((1, 2), {'function': 'hello'})])

def test_run_module_cleanUp_without_teardown(self):
ordering = []

class Module(object):
unittest.addModuleCleanup(print, 'module cleanup')

class TestableTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
ordering.append('setUpClass')
def testNothing(self):
ordering.append('test')
@classmethod
def tearDownClass(cls):
ordering.append('tearDownClass')

TestableTest.__module__ = 'Module'
sys.modules['Module'] = Module
runTests(TestableTest)
self.assertEqual(ordering, ['setUpClass', 'test', 'tearDownClass'])
self.assertEqual(unittest.case._module_cleanups, [])

def test_run_module_cleanUp_when_teardown_exception(self):
ordering = []

class Module(object):
unittest.addModuleCleanup(print, 'module cleanup')

@staticmethod
def tearDownModule():
raise Exception('CleanUpExc')

class TestableTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
ordering.append('setUpClass')
def testNothing(self):
ordering.append('test')
@classmethod
def tearDownClass(cls):
ordering.append('tearDownClass')

TestableTest.__module__ = 'Module'
sys.modules['Module'] = Module
runTests(TestableTest)
self.assertEqual(ordering, ['setUpClass', 'test', 'tearDownClass'])
self.assertEqual(unittest.case._module_cleanups, [])

def test_run_module_cleanUp(self):
blowUp = True
ordering = []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the :func:`unittest.addModuleCleanup` behavior. It is now called even if :meth:`tearDownModule` is not defined.