-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Add addCleanup
to unittest.subTest
#134079
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
Comments
(If anyone wants to take it, go ahead! Otherwise I'll get to this eventually.) |
I can get to this in the evening:-) It's suprising that this was not implemented before! |
If it's alright with you guys, I'd love to take on this issue. |
My implementation is not idea since _SubTest is a subclass and would therefore also expose asserts. I'd be interested to see what you have planned. |
I'm thinking of creating a small helper class that only handles cleanup methods — |
I thought about that, but IMO it is pretty messy with a lot of duplicated code, so I don’t know. It is probably the only option. |
Yes, it will be messy with duplicated code but I guess it's better than exposing unnecessary functionality. I also not able to think of any other way. |
@encukou What are your thoughts on the implementation for this issue? Would love to hear your thoughts or suggestions. |
My 2c:
The first solution would at least make any (CPython) code that is currently using |
I think it's best to leave Don't worry about duplicated code until the solution is working and tested. Then it's time to deduplicate. |
Three ways to solve this issue was mentioned on the Discuss thread. With examples:
for param in 'a', 'b', 'c':
with self.subTest() as sub:
tempfile = make_tempfile()
sub.addCleanup(os.unlink, tempfile)
do_actual_test(tempfile, param) But we need to add asynchronous variants to
for param in 'a', 'b', 'c':
with self.subTest():
tempfile = make_tempfile()
self.addSubTestCleanup(os.unlink, tempfile)
do_actual_test(tempfile, param) It may be easier to implement for
for param in 'a', 'b', 'c':
with self.subTest(), self.fence():
tempfile = make_tempfile()
self.addCleanup(os.unlink, tempfile)
do_actual_test(tempfile, param)
But we will be able to fully understand the advantages and disadvantages only when we try to implement all three options. |
Uh oh!
There was an error while loading. Please reload this page.
Feature or enhancement
Proposal:
The
unittest.subTest
context (i.e. the object you get usingwith
'sas
clause, currentlyNone
) should get methods to manage cleanups:addCleanup
enterContext
doCleanups
And similar ones for
IsolatedAsyncIOTestCase
.They should do the same thing as the same methods on
TestCase
, but with subtest scope.Example usage:
See Discuss thread for motivation/discussion.
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/unittest-add-addcleanup-to-subtest/91827
Linked PRs
addCleanup
,enterContext
anddoCleanups
tounittest.subTest
and tests #134318The text was updated successfully, but these errors were encountered: