Skip to content

unittest - use contexts to assert exceptions #48694

Closed

Description

@dleonard0
mannequin
BPO 4444
Nosy @pitrou
Files
  • unittest.patch: patych to unittest.py
  • 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 = <Date 2008-12-28.14:29:38.952>
    created_at = <Date 2008-11-27.09:49:59.368>
    labels = ['type-feature', 'library']
    title = 'unittest - use contexts to assert exceptions'
    updated_at = <Date 2008-12-28.14:29:38.951>
    user = 'https://bugs.python.org/dleonard0'

    bugs.python.org fields:

    activity = <Date 2008-12-28.14:29:38.951>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2008-12-28.14:29:38.952>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2008-11-27.09:49:59.368>
    creator = 'dleonard0'
    dependencies = []
    files = ['12138']
    hgrepos = []
    issue_num = 4444
    keywords = ['patch']
    message_count = 3.0
    messages = ['76492', '76500', '78404']
    nosy_count = 3.0
    nosy_names = ['purcell', 'pitrou', 'dleonard0']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue4444'
    versions = ['Python 3.0']

    Activity

    1. dleonard0 commented on Nov 27, 2008

      dleonard0mannequin
      MannequinAuthor

      Patch to allow unit tests to test for exceptions through a 'with'
      statement. Resulting (failing) test would look like this:

        import unittest
      
        class T(unittest.TestCase):
           def runTest(self):
              with self.assertRaises(KeyError):
                 raise ValueError

      This saves having to put exception raising tests into a try/except
      block, which is cool. And by cool I mean totally sweet.

    2. added
      stdlib
      type-feature
      on Nov 27, 2008
    3. purcell commented on Nov 27, 2008

      purcellmannequin
      Mannequin

      I like this change, since assertRaises can be a bit messy when passing it
      a local function.

      I'd suggest modifying the patch such that the AssertRaisesContext class is
      also used when callableObj is provided, which would eliminate the
      duplication of the code that checks for exceptions.

    4. pitrou commented on Dec 28, 2008

      @pitrou
      Member

      I've committed an improved patch, with tests and doc, in r67979 and
      r67981. Thanks!

    5. transferred this issue fromon Apr 10, 2022
    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

      stdlibtype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions