[New-bugs-announce] [issue9587] unittest.assertRaises() return the raised exception

Denver Coneybeare report at bugs.python.org
Fri Aug 13 16:57:44 CEST 2010

New submission from Denver Coneybeare <denver.coneybeare at gmail.com>:

It would be great if unittest.assertRaises() returned the raised exception when it passes.  This allows the caller to easily perform further checks on the exception, such as its attribute values.  Currently assertRaises() returns None (when it doesn't return a context manager) so changing the return value should not break backwards compatibility.

I see that this was already discussed in issue6275 but I'd like to resurrect the discussion since this is a common scenario in my unit tests, and I assume others.  Revisions r76238 and r78110 added the ability to get the exception from the context manager (good) but sometimes using the context manager approach adds unnecessary bloat to already long-winded unit tests.

I've attached a possible patch for the py3k branch (unittest.assertRaises.returnex.v1.patch).  Thank you for (re)considering this topic :)  Also, thank you Michael Foord for your recent improvements to unittest... the new features are very much appreciated!

components: Library (Lib)
files: unittest.assertRaises.returnex.v1.patch
keywords: patch
messages: 113781
nosy: denversc, krisvale, michael.foord
priority: normal
severity: normal
status: open
title: unittest.assertRaises() return the raised exception
type: feature request
versions: Python 3.2
Added file: http://bugs.python.org/file18501/unittest.assertRaises.returnex.v1.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list