classification
Title: unittest module cleanup functions not run unless tearDownModule() is defined
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, michael.foord, miguendes, python-dev, rbcollins, rtarpine, terry.reedy
Priority: normal Keywords: patch

Created on 2021-04-22 17:30 by rtarpine, last changed 2021-05-03 07:27 by miguendes.

Pull Requests
URL Status Linked Edit
PR 25700 open python-dev, 2021-04-28 22:19
Messages (6)
msg391619 - (view) Author: Ryan Tarpine (rtarpine) Date: 2021-04-22 17:30
Functions registered with unittest.addModuleCleanup are not called unless the user defines tearDownModule in their test module.

This behavior is unexpected because functions registered with TestCase.addClassCleanup are called even the user doesn't define tearDownClass, and similarly with addCleanup/tearDown.

The implementing code is basically the same for all 3 cases, the difference is that unittest.TestCase itself defines tearDown and tearDownClass; so even though doClassCleanups is only called if tearDownClass is defined, in practice it always is.

doModuleCleanups should be called even if tearDownModule is not defined.
msg391800 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-04-24 20:34
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?
msg392268 - (view) Author: Miguel Brito (miguendes) * Date: 2021-04-28 23:23
Hello, first time here. I created an PR for that. Managed to reproduce the issue both manually and via unit test.

I hope there's no edge case but all tests pass on my machine.
msg392580 - (view) Author: Miguel Brito (miguendes) * Date: 2021-05-01 09:59
I was reading through the dev guide and past issues and I didn't know it's advisable to give the author of the issue a chance to submit the PR.

Sorry about that, you can close mine in this case.
msg392590 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-05-01 12:42
No apology needed.  I don't know what has been added to the devguide, but most OPs never submit a PR unless they either do so or say they will when opening an issue.  In any case, a week is more than a chance.
msg392773 - (view) Author: Miguel Brito (miguendes) * Date: 2021-05-03 07:27
Thanks terry.reedy, actually I read it in the mailing list, someones comment not a guideline.

Do you mind having a look at the PR?
History
Date User Action Args
2021-05-03 07:27:00miguendessetmessages: + msg392773
2021-05-01 12:42:11terry.reedysetmessages: + msg392590
2021-05-01 09:59:31miguendessetmessages: + msg392580
2021-04-28 23:23:25miguendessetnosy: + miguendes
messages: + msg392268
2021-04-28 22:19:40python-devsetkeywords: + patch
nosy: + python-dev

pull_requests: + pull_request24390
stage: needs patch -> patch review
2021-04-24 20:34:45terry.reedysetstage: needs patch
versions: + Python 3.9, Python 3.10
2021-04-24 20:34:29terry.reedysetnosy: + ezio.melotti, terry.reedy, michael.foord, rbcollins
messages: + msg391800
2021-04-22 17:30:03rtarpinecreate