Title: Add `addCleanup` to `unittest.subTest` · Issue #134079 · python/cpython · GitHub
Open Graph Title: Add `addCleanup` to `unittest.subTest` · Issue #134079 · python/cpython
X Title: Add `addCleanup` to `unittest.subTest` · Issue #134079 · python/cpython
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: addCleanup enterContext doCleanups And similar ones for IsolatedAsyncIO...
Open Graph 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: addCleanup enterContext doClean...
X 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: addCleanup enterContext doC...
Opengraph URL: https://github.com/python/cpython/issues/134079
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add `addCleanup` to `unittest.subTest`","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nThe `unittest.subTest` context (i.e. the object you get using `with`'s `as` clause, currently `None`) should get methods to manage cleanups:\n\n- `addCleanup`\n- `enterContext`\n- `doCleanups`\n\nAnd similar ones for `IsolatedAsyncIOTestCase`.\n\nThey should do the same thing as the same methods on `TestCase`, but with subtest scope.\nExample usage:\n```python\nfor param in 'a', 'b', 'c':\n with self.subTest() as sub:\n tempfile = make_tempfile()\n sub.addCleanup(os.unlink, tempfile)\n do_actual_test(tempfile, param)\n```\n\nSee [Discuss thread](https://discuss.python.org/t/unittest-add-addcleanup-to-subtest/91827) for motivation/discussion.\n\n### Has this already been discussed elsewhere?\n\nI have already discussed this feature proposal on Discourse\n\n### Links to previous discussion of this feature:\n\nhttps://discuss.python.org/t/unittest-add-addcleanup-to-subtest/91827\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-134318\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/encukou","@type":"Person","name":"encukou"},"datePublished":"2025-05-16T06:44:49Z","url":"https://github.com/python/cpython/issues/134079"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:54845be2-3fe4-f543-9429-9c5f398056ec |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DFCA:21BD98:6827F7:8380BD:6B011586 |
| html-safe-nonce | 80b8d64e00b5656e0c9d4cbb008fc77094fecd29f58c5d0bb11b433822068b13 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERkNBOjIxQkQ5ODo2ODI3Rjc6ODM4MEJEOjZCMDExNTg2IiwidmlzaXRvcl9pZCI6IjMxMjM5MTgwNTkwODE4OTczNTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b364a1ebe6a7f62a1bd6f076234d02f5de1d0709bd4aaa7a773d0e80d6e40a5b |
| hovercard-subject-tag | issue:3068101016 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/134079/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a9442447a9b15169bd49f391c760251751f8db0c8cda35804a89517771686f51/python/cpython/issues/134079 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a9442447a9b15169bd49f391c760251751f8db0c8cda35804a89517771686f51/python/cpython/issues/134079 |
| og:image:alt | 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: addCleanup enterContext doClean... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | encukou |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width