Skip to content

Added helper to test.support for parameterizing tests #135120

Closed

Description

@serhiy-storchaka

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) and their values (an iterable of values for parameters):

@subTests('a,b', [('foo', 2), ('bar', 5)])
def test_foo(self, a, b):
    ...

is equivalent to

def test_foo(self):
    for a, b in [('foo', 2), ('bar', 5)]:
        with self.subTest(a=a, b=b):
            ...

It allows to save 2 levels of indentation.

Multiple decorators can be used, this generates a Descartes production of parameter values:

@subTests('a,b', [('foo', 2), ('bar', 5)])
@subTests('c', range(5))
def test_foo(self, a, b, c):
    ...

is equivalent to

def test_foo(self):
    for a, b in [('foo', 2), ('bar', 5)]:
        with self.subTest(a=a, b=b):
            for c in range(5):
                with self.subTest(c=c):
                    ...

It allows to save even more 2 levels of indentation.

It 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.

There are differences from "real" parameterization:

  • setUp and tearDown are only run once, before and after all subtests.
  • It is impossible to select or filter out tests with specific parameters.
  • It can only parametrize test methods, not classes.

I 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.

The name subTests is intentionally chosen to not confuse with parametrize which is reserved for real test parameterization.

The PR contains also several examples of using that decorator.

Linked PRs

Activity

  1. added
    3.13only security fixes
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Jun 4, 2025
  2. added 3 commits that reference this issue on Jun 4, 2025
  3. ZeroIntensity commented on Jun 4, 2025

    @ZeroIntensity
    Member

    @picnixz I think you suggested this exact thing a little while ago!

  4. added a commit that references this issue on Jun 6, 2025
  5. added 4 commits that reference this issue on Jun 6, 2025
  6. picnixz commented on Jun 7, 2025

    @picnixz
    Member
  7. 3 remaining items

  8. added a commit that references this issue on Jul 4, 2025
  9. added 3 commits that reference this issue on Jul 4, 2025
  10. added 3 commits that reference this issue on Jul 12, 2025
  11. added a commit that references this issue on Jul 12, 2025
  12. added a commit that references this issue on Jul 30, 2025
  13. added a commit that references this issue on Aug 4, 2025
  14. added a commit that references this issue on Aug 19, 2025
  15. StanFromIreland commented on Oct 23, 2025

    @StanFromIreland
    Member

    Triage: PR & backports have been merged, can this be closed?

  16. added
    pendingThe issue will be closed if no feedback is provided
    on Oct 27, 2025
  17. removed
    pendingThe issue will be closed if no feedback is provided
    on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixes3.15bugs and security fixestestsTests in the Lib/test dirtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions