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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy