Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-134079: AddaddCleanup
,enterContext
anddoCleanups
tounittest.subTest
and tests#134318
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
python-cla-botbot commentedMay 20, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
cc@encukou
Lib/unittest/case.py Outdated
@@ -1626,3 +1629,46 @@ def shortDescription(self): | |||
def __str__(self): | |||
return "{} {}".format(self.test_case, self._subDescription()) | |||
class _SubTestCleanupHelper(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
class_SubTestCleanupHelper(): | |
class_SubTestCleanupHelper: |
Lib/unittest/case.py Outdated
def _callCleanup(self, function, /, *args, **kwargs): | ||
function(*args, **kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
def_callCleanup(self,function,/,*args,**kwargs): | |
function(*args,**kwargs) | |
@staticmethod | |
def_callCleanup(function,/,*args,**kwargs): | |
function(*args,**kwargs) |
I don't think we need to hold a reference toself
so a staticmethod can be used but OTOH, I don't know if there is a world where we want to be able to subclass this one in the future.
if hasattr(TestSubTestCleanups, '_active_instance'): | ||
del TestSubTestCleanups._active_instance | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
ifhasattr(TestSubTestCleanups,'_active_instance'): | |
delTestSubTestCleanups._active_instance | |
ifhasattr(TestSubTestCleanups,'_active_instance'): | |
delTestSubTestCleanups._active_instance | |
ArunRawat404May 21, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Can you explain this one i don't see any difference
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
We use 2 blank lines to separate toplevel classes/functions and 1 line for methods.
] | ||
self.assertEqual(self.events, expected_events) | ||
def tearDown(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Can you put the tearDown() after the setUp() instead? it'll be easier to maintain and read.
Summary
gh-134079: Add
addCleanup
,enterContext
anddoCleanups
tounittest.subTest
and add corresponding testsDescription
This PR implements support for
addCleanup
,enterContext
, anddoCleanups
inunittest.subTest()
contexts, as discussed inissue-134079. This enables users to register cleanups or use context managers inside a withself.subTest():
block, similar to how it's done inTestCase
Tests
Added tests in
test_runner.py
to validate:enterContext()
behavior on success/failureI'm still learning the internals of CPython and writing tests, so please don't hesitate to suggest improvements. I'd really appreciate any feedback.