Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-134565: Use ExceptionGroup to handle multiple errors in unittest.doModuleCleanups()#134566
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
Changes from1 commit
c06caac
5ae08b0
0bd3333
7207643
fb27d61
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
…oModuleCleanup()
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -651,6 +651,39 @@ class Module(object): | ||
self.assertEqual(str(e.exception), 'CleanUpExc') | ||
self.assertEqual(unittest.case._module_cleanups, []) | ||
def test_doModuleCleanup_with_multiple_errors_in_addModuleCleanup(self): | ||
def module_cleanup_bad1(): | ||
raise TypeError('CleanUpExc1') | ||
def module_cleanup_bad2(): | ||
raise ValueError('CleanUpExc2') | ||
class Module(object): | ||
unittest.addModuleCleanup(module_cleanup_bad1) | ||
unittest.addModuleCleanup(module_cleanup_bad2) | ||
with self.assertRaises(ExceptionGroup) as e: | ||
unittest.case.doModuleCleanups() | ||
e = e.exception | ||
self.assertEqual(str(e), 'module cleanup failed (2 sub-exceptions)') | ||
self.assertEqual(str(e.exceptions[0]), 'CleanUpExc2') | ||
self.assertEqual(str(e.exceptions[1]), 'CleanUpExc1') | ||
def test_doModuleCleanup_with_exception_group_in_addModuleCleanup(self): | ||
def module_cleanup_bad(): | ||
raise ExceptionGroup('CleanUpExc', [TypeError('CleanUpExc1'), | ||
ValueError('CleanUpExc2')]) | ||
class Module(object): | ||
unittest.addModuleCleanup(module_cleanup_bad) | ||
with self.assertRaises(ExceptionGroup) as e: | ||
unittest.case.doModuleCleanups() | ||
e = e.exception | ||
self.assertEqual(str(e), 'module cleanup failed (1 sub-exception)') | ||
e = e.exceptions[0] | ||
self.assertEqual(str(e), 'CleanUpExc (2 sub-exceptions)') | ||
self.assertEqual(str(e.exceptions[0]), 'CleanUpExc1') | ||
self.assertEqual(str(e.exceptions[1]), 'CleanUpExc2') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. We have a utility for this check: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Done. | ||
def test_addModuleCleanup_arg_errors(self): | ||
cleanups = [] | ||
def cleanup(*args, **kwargs): | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -149,9 +149,9 @@ def doModuleCleanups(): | ||
except Exception as exc: | ||
exceptions.append(exc) | ||
if exceptions: | ||
if len(exceptions) == 1 and not isinstance(exceptions[0], ExceptionGroup): | ||
raise exceptions[0] | ||
raiseExceptionGroup('module cleanup failed',exceptions) | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
def skip(reason): | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
:func:`unittest.doModuleCleanup` no longer swallows all but first exception | ||
raised in the cleanup code, but raises a :exc:`ExceptionGroup` if multiple | ||
errors occurred. |
Uh oh!
There was an error while loading.Please reload this page.