This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients ezio.melotti, michael.foord, rbcollins, rtarpine, terry.reedy
Date 2021-04-24.20:34:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1619296469.28.0.698262156642.issue43913@roundup.psfhosted.org>
In-reply-to
Content
import unittest

#def setUpModule(): raise Exception()
#def tearDownModule(): print('module teardown')

unittest.addModuleCleanup(print, 'module cleanup')

class Dummy(unittest.TestCase):
    def test_dummy(self):
        self.addCleanup(print, 'test cleanup')
        self.addClassCleanup(print, 'class cleanup')

unittest.main()
----------------------------------------------
Above verifies the claim in 3.10.0a7.   Only 'test cleanup' and 'class cleanup' print. Uncomment either function and 'module cleanup' is also printed.

https://docs.python.org/3/library/unittest.html#unittest.addModuleCleanu
"""
unittest.addModuleCleanup(function, /, *args, **kwargs)

    Add a function to be called after tearDownModule() to cleanup resources used during the test class. Functions will be called in reverse order to the order they are added (LIFO). They are called with any arguments and keyword arguments passed into addModuleCleanup() when they are added.

    If setUpModule() fails, meaning that tearDownModule() is not called, then any cleanup functions added will still be called.
"""
This seems slightly ambiguous as to behavior when no tearDownModule.  However, the doc for addClassCleanup is exactly parallel.

https://docs.python.org/3/library/unittest.html#unittest.TestCase.addClassCleanup
"""
classmethod addClassCleanup(function, /, *args, **kwargs)

    Add a function to be called after tearDownClass() to cleanup resources used during the test class. Functions will be called in reverse order to the order they are added (LIFO). They are called with any arguments and keyword arguments passed into addClassCleanup() when they are added.

    If setUpClass() fails, meaning that tearDownClass() is not called, then any cleanup functions added will still be called.
"""

The doc for addCleanup is also parallel.  The behavior difference therefore seems like a bug and calling in any case seems more correct.

Ryan, can you prepare a PR?
History
Date User Action Args
2021-04-24 20:34:29terry.reedysetrecipients: + terry.reedy, rbcollins, ezio.melotti, michael.foord, rtarpine
2021-04-24 20:34:29terry.reedysetmessageid: <1619296469.28.0.698262156642.issue43913@roundup.psfhosted.org>
2021-04-24 20:34:29terry.reedylinkissue43913 messages
2021-04-24 20:34:29terry.reedycreate