-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unittest module cleanup functions not run unless tearDownModule() is defined #88079
Comments
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. |
import unittest #def setUpModule(): raise Exception() 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
""" https://docs.python.org/3/library/unittest.html#unittest.TestCase.addClassCleanup
""" 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? |
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. |
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. |
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. |
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? |
Functions registered with TestCase.addClassCleanup are called because TestCase has default implementation of tearDownClass. But you can suppress it by setting tearDownClass = None. So there is a similar issue with class cleanup functions. |
There are several other bugs in the code for cleaning up classes and modules:
And several lesser bugs. |
Thanks for the fix, Serhiy, and Ryan for reporting the problem! ✨ 🍰 ✨ |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: