Add `addCleanup` to `unittest.subTest`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Feature or enhancement
Proposal:
The unittest.subTest context (i.e. the object you get using with's as clause, currently None) should get methods to manage cleanups:
addCleanupenterContextdoCleanups
And similar ones for IsolatedAsyncIOTestCase.
They should do the same thing as the same methods on TestCase, but with subtest scope.
Example usage:
for param in 'a', 'b', 'c':
with self.subTest() as sub:
tempfile = make_tempfile()
sub.addCleanup(os.unlink, tempfile)
do_actual_test(tempfile, param)
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
- gh-134318
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the existing unittest.TestCase cleanup methods and the unittest.subTest context, then compare the corresponding IsolatedAsyncIOTestCase behavior. Read the Discourse thread and linked PR gh-134318 to understand the proposed scope. Done means the requested cleanup methods work with subtest scope for both synchronous and isolated asynchronous cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100