You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
All TestCase classes use a shared stack for class cleanup #99645
TestCase class methods addClassCleanup() and doClassCleanups() are similar to instance methods addCleanup() and doCleanups(). add*Cleanup() add a callback into a list, and do*Cleanups() pop them from a list and call. The main difference is that the latter methods use a list stored as an instance attribute while the former use a list stored as a class attribute.
The problem is that the class attribute TestCase._class_cleanups is shared between all TestCase subclasses. It usually does not cause the problem, because tests in different test classes are run sequentially, but if you run a new test suite while running a test, and the outer test class use addClassCleanup(), the callback registered for the outer test class will be called when cleaning up the inner test class. It can happen when you test unittest itself. It really happened, and was unnoticed only because the outer class did not use addClassCleanup().
Are there existing tests for class cleanups added in various levels of mixins and/or TestCase-derived base classes? I've not gone looking in the hope that you have a quick "yes" answer :)
Just concerned that this could (though, AFAICT, shouldn't) have an effect on classes that inherit from multiple bases with class-level cleanups. This is a pattern I use frequently, and which came to mind when I saw the change notice.
It works in a different way. If you define setUpClass() which calls addClassCleanup() in the base class, and call Base.setUpClass() in setUpClass() of derived classes, Base.setUpClass() will be called multiple times, once for every derived test classes (and for the base class if it is a test class). Correspondingly the cleanup function will be called for every test class. Nothing was changed with this PR.
Activity
pythongh-99645: Fix a bug in handling class cleanups in unittest.Test…
gh-99645: Fix a bug in handling class cleanups in unittest.TestCase (G…
[3.11] pythongh-99645: Fix a bug in handling class cleanups in unitte…
[3.11] pythongh-99645: Fix a bug in handling class cleanups in unitte…
[3.10] pythongh-99645: Fix a bug in handling class cleanups in unitte…
[3.11] gh-99645: Fix a bug in handling class cleanups in unittest.Tes…
zware commentedon Nov 22, 2022
Are there existing tests for class cleanups added in various levels of mixins and/or
TestCase-derived base classes? I've not gone looking in the hope that you have a quick "yes" answer :)[3.10] gh-99645: Fix a bug in handling class cleanups in unittest.Tes…
serhiy-storchaka commentedon Nov 22, 2022
AFAIK there are no such tests. But how is this related to this issue?
zware commentedon Nov 22, 2022
Just concerned that this could (though, AFAICT, shouldn't) have an effect on classes that inherit from multiple bases with class-level cleanups. This is a pattern I use frequently, and which came to mind when I saw the change notice.
serhiy-storchaka commentedon Nov 22, 2022
It works in a different way. If you define
setUpClass()which callsaddClassCleanup()in the base class, and callBase.setUpClass()insetUpClass()of derived classes,Base.setUpClass()will be called multiple times, once for every derived test classes (and for the base class if it is a test class). Correspondingly the cleanup function will be called for every test class. Nothing was changed with this PR.