Title: Added helper to test.support for parameterizing tests · Issue #135120 · python/cpython · GitHub
Open Graph Title: Added helper to test.support for parameterizing tests · Issue #135120 · python/cpython
X Title: Added helper to test.support for parameterizing tests · Issue #135120 · python/cpython
Description: This is a simple decorator modeled after pytest.mark.parametrize. It allows to "parametrize" tests, each parametrized test is run as a subtest. The decorator specifies the name(s) of parameters (as an iterable or a comma separated string...
Open Graph Description: This is a simple decorator modeled after pytest.mark.parametrize. It allows to "parametrize" tests, each parametrized test is run as a subtest. The decorator specifies the name(s) of parameters (as...
X Description: This is a simple decorator modeled after pytest.mark.parametrize. It allows to "parametrize" tests, each parametrized test is run as a subtest. The decorator specifies the name(s) of para...
Opengraph URL: https://github.com/python/cpython/issues/135120
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Added helper to test.support for parameterizing tests","articleBody":"This is a simple decorator modeled after `pytest.mark.parametrize`. It allows to \"parametrize\" tests, each parametrized test is run as a subtest.\n\nThe decorator specifies the name(s) of parameters (as an iterable or a comma separated string) and their values (an iterable of values for parameters):\n\n```py\n@subTests('a,b', [('foo', 2), ('bar', 5)])\ndef test_foo(self, a, b):\n ...\n```\nis equivalent to\n```py\ndef test_foo(self):\n for a, b in [('foo', 2), ('bar', 5)]:\n with self.subTest(a=a, b=b):\n ...\n```\nIt allows to save 2 levels of indentation.\n\nMultiple decorators can be used, this generates a Descartes production of parameter values:\n```py\n@subTests('a,b', [('foo', 2), ('bar', 5)])\n@subTests('c', range(5))\ndef test_foo(self, a, b, c):\n ...\n```\nis equivalent to\n```py\ndef test_foo(self):\n for a, b in [('foo', 2), ('bar', 5)]:\n with self.subTest(a=a, b=b):\n for c in range(5):\n with self.subTest(c=c):\n ...\n```\nIt allows to save even more 2 levels of indentation.\n\nIt has also optional keyword-only parameter `_do_cleanups`. `_do_cleanups=True` tells to call `doCleanups()` after each subtest. This is temporary feature, only for use in test_ntpath. In general, it is wrong to do this because `doCleanups()` will also calls callbacks added in `setUp`. It will be replaced either by `addSubTestCleanup()` (see https://discuss.python.org/t/unittest-add-addcleanup-to-subtest/91827 and #134079) or by other methods (for example by adding fences for cleanup). Also, it will not be needed for real test parameterization.\n\nThere are differences from \"real\" parameterization:\n\n* `setUp` and `tearDown` are only run once, before and after all subtests.\n* It is impossible to select or filter out tests with specific parameters.\n* It can only parametrize test methods, not classes.\n\nI plan to implement real parameterization, this is why I only propose to add that decorator in `test.support`, not in `unittest`. In any case it would be backported in `test.support`. We may finally add both decorators in `unittest` if this does not create confusion.\n\nThe name `subTests` is intentionally chosen to not confuse with `parametrize` which is reserved for real test parameterization.\n\nThe PR contains also several examples of using that decorator.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-135121\n* gh-135208\n* gh-135210\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/serhiy-storchaka","@type":"Person","name":"serhiy-storchaka"},"datePublished":"2025-06-04T09:46:33Z","url":"https://github.com/python/cpython/issues/135120"}
| 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:cb7858c0-d94f-6e4e-1d03-8ea47f7173a8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C284:4279A:EDD8AC:1406A20:6B0112CE |
| html-safe-nonce | 9eeeb6f40c8440499db1113ddf17f7ebe50dbb7ce64da289de417e332f075081 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMjg0OjQyNzlBOkVERDhBQzoxNDA2QTIwOjZCMDExMkNFIiwidmlzaXRvcl9pZCI6IjQ4NTY4OTE3NTA0NjUyMTMxMzQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 88dcbeca360b06c6bb5b06956e6edec6ae3556b65bd309fbe266d16db9a498d5 |
| hovercard-subject-tag | issue:3117199772 |
| 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/135120/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a619986dd0c2332450cc518f8182a9893c65ad7f1527e68ed530c139e99fab02/python/cpython/issues/135120 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a619986dd0c2332450cc518f8182a9893c65ad7f1527e68ed530c139e99fab02/python/cpython/issues/135120 |
| og:image:alt | This is a simple decorator modeled after pytest.mark.parametrize. It allows to "parametrize" tests, each parametrized test is run as a subtest. The decorator specifies the name(s) of parameters (as... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | serhiy-storchaka |
| 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