8000 Add `addCleanup` to `unittest.subTest` · Issue #134079 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Add addCleanup to unittest.subTest #134079
Open
@encukou

Description

@encukou

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0