This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: unittest - use contexts to assert exceptions
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.0
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: dleonard0, pitrou, purcell
Priority: normal Keywords: patch

Created on 2008-11-27 09:49 by dleonard0, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
unittest.patch dleonard0, 2008-11-27 09:49 patych to
Messages (3)
msg76492 - (view) Author: David Leonard (dleonard0) Date: 2008-11-27 09:49
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.
msg76500 - (view) Author: Steve Purcell (purcell) (Python triager) Date: 2008-11-27 14:07
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.
msg78404 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-12-28 14:29
I've committed an improved patch, with tests and doc, in r67979 and
r67981. Thanks!
Date User Action Args
2022-04-11 14:56:41adminsetgithub: 48694
2008-12-28 14:29:38pitrousetstatus: open -> closed
resolution: fixed
messages: + msg78404
2008-12-27 21:05:37pitrousetnosy: + pitrou
2008-11-27 14:07:41purcellsetnosy: + purcell
messages: + msg76500
2008-11-27 09:49:59dleonard0create