-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
bpo-24412: Adds cleanUps for setUpClass and setUpModule. #9190
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
Changes from all commits
e6756cb
5332a2c
7939f14
162b6b2
47162c3
65e2a71
75c902f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,30 @@ def testPartExecutor(self, test_case, isTest=False): | |
def _id(obj): | ||
return obj | ||
|
||
|
||
_module_cleanups = [] | ||
def addModuleCleanup(function, *args, **kwargs): | ||
"""Same as addCleanup, except the cleanup items are called even if | ||
setUpModule fails (unlike tearDownModule).""" | ||
_module_cleanups.append((function, args, kwargs)) | ||
|
||
|
||
def doModuleCleanups(): | ||
"""Execute all module cleanup functions. Normally called for you after | ||
tearDownModule.""" | ||
exceptions = [] | ||
while _module_cleanups: | ||
function, args, kwargs = _module_cleanups.pop() | ||
try: | ||
function(*args, **kwargs) | ||
except Exception as exc: | ||
exceptions.append(exc) | ||
if exceptions: | ||
# Swallows all but first exception. If a multi-exception handler | ||
# gets written we should use that here instead. | ||
raise exceptions[0] | ||
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. I totally get why you have to do it this way, although it would be nice if we had trio's MultiError in the stdlib. :) It's unfortunate that we have to throw away all the other exceptions, but I don't know where you can stash them. E.g. IIUC, instance cleanups stash the exceptions on It seems like a lot of extra complication for hopefully an, um, exceptional case, so maybe punt on that for now. IOW, I'm cool with this, unless you have any other ideas. |
||
|
||
|
||
def skip(reason): | ||
""" | ||
Unconditionally skip a test. | ||
|
@@ -390,6 +414,8 @@ class TestCase(object): | |
|
||
_classSetupFailed = False | ||
|
||
_class_cleanups = [] | ||
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. Minor nit (maybe!): In this case, the blank line between these two class attributes may not be needed. |
||
|
||
def __init__(self, methodName='runTest'): | ||
"""Create an instance of the class that will use the named test | ||
method when executed. Raises a ValueError if the instance does | ||
|
@@ -445,6 +471,12 @@ def addCleanup(self, function, *args, **kwargs): | |
Cleanup items are called even if setUp fails (unlike tearDown).""" | ||
self._cleanups.append((function, args, kwargs)) | ||
|
||
@classmethod | ||
def addClassCleanup(cls, function, *args, **kwargs): | ||
"""Same as addCleanup, except the cleanup items are called even if | ||
setUpClass fails (unlike tearDownClass).""" | ||
cls._class_cleanups.append((function, args, kwargs)) | ||
|
||
def setUp(self): | ||
"Hook method for setting up the test fixture before exercising it." | ||
pass | ||
|
@@ -651,9 +683,21 @@ def doCleanups(self): | |
function(*args, **kwargs) | ||
|
||
# return this for backwards compatibility | ||
# even though we no longer us it internally | ||
# even though we no longer use it internally | ||
return outcome.success | ||
|
||
@classmethod | ||
def doClassCleanups(cls): | ||
"""Execute all class cleanup functions. Normally called for you after | ||
tearDownClass.""" | ||
cls.tearDown_exceptions = [] | ||
while cls._class_cleanups: | ||
function, args, kwargs = cls._class_cleanups.pop() | ||
try: | ||
function(*args, **kwargs) | ||
except Exception as exc: | ||
cls.tearDown_exceptions.append(sys.exc_info()) | ||
|
||
def __call__(self, *args, **kwds): | ||
return self.run(*args, **kwds) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.