Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of context managers in unittest #89209

Closed
serhiy-storchaka opened this issue Aug 29, 2021 · 2 comments
Closed

Add support of context managers in unittest #89209

serhiy-storchaka opened this issue Aug 29, 2021 · 2 comments
Labels
3.11 stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Aug 29, 2021

BPO 45046
Nosy @rbtcollins, @ezio-melotti, @bitdancer, @voidspace, @cjerdonek, @serhiy-storchaka
PRs
  • bpo-45046: Support context managers in unittest #28045
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2021-08-29.16:56:08.979>
    labels = ['type-feature', 'library', '3.11']
    title = 'Add support of context managers in unittest'
    updated_at = <Date 2021-08-29.17:01:15.427>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2021-08-29.17:01:15.427>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-08-29.16:56:08.979>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45046
    keywords = ['patch']
    message_count = 1.0
    messages = ['400552']
    nosy_count = 6.0
    nosy_names = ['rbcollins', 'ezio.melotti', 'r.david.murray', 'michael.foord', 'chris.jerdonek', 'serhiy.storchaka']
    pr_nums = ['28045']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue45046'
    versions = ['Python 3.11']

    @serhiy-storchaka
    Copy link
    Member Author

    serhiy-storchaka commented Aug 29, 2021

    Methods setUp() and tearDown() of TestClass allow to add some code executed before and after every test method. In many cases addCleanup() is more convenient than tearDown() -- you do not need to keep data for cleaning up as TestCase attributes, addCleanup() doe it for you. You should not worry about partial cleaning up if setUp() fails in the middle. You can also use addCleanup() in test methods, and corresponding resources will be cleaned only for these tests which created them.

        resource = open_resource()
        self.addCleanup(close_resource, resource)
        self.resource = resource  # optional, if you need access to it in test methods

    Some resources are managed by context managers. It is so easy to create a context manager with the contextlib.contextmanager decorator, that its __enter__ and __exit__ methods can be only way to create and destroy resource. So the code looks like the following:

        cm = my_context_manager()
        cm.__enter__()
        # or self.resource = cm.__enter__()
        self.addCleanup(cm.__exit__, None, None, None)

    It looks not so nice. You need to use dunder methods, and pass thee Nones as arguments for __exit__.

    I propose to add helpers: methods enterContext(), enterClassContext(), enterAsyncContext() and function enterModuleContext() which wraps addCleanup/addClassCleanup/addAsyncCleanup/addModuleCleanup correspondently and allow to get rid of the boilerplate code. Example:

        self.enterContext(my_context_manager())
        # or self.resource = self.enterContext(my_context_manager())

    It solves the same problem as bpo-15351, but from different direction, so I opened a separate issue.

    @serhiy-storchaka serhiy-storchaka added 3.11 stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Aug 29, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @adamchainz
    Copy link
    Contributor

    adamchainz commented Nov 13, 2022

    This can be closed, the feature has been merged and released.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    Status: Done
    Development

    No branches or pull requests

    3 participants