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.

classification
Title: let unittest.assertRaises() return the exception object caught
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: michael.foord Nosy List: draghuram, exarkun, kristjan.jonsson, michael.foord
Priority: normal Keywords: easy, needs review, patch

Created on 2009-06-12 21:43 by kristjan.jonsson, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
unittest.patch kristjan.jonsson, 2009-06-12 21:43
unitest2.patch kristjan.jonsson, 2009-06-14 16:52
Messages (10)
msg89304 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2009-06-12 21:43
It can be useful, after a unittest.assertRaises() or assertRaisesRegexp() 
to be able to take a closer look at the exception that was raised.
To this end, I propose returning the caught exception from these methods.

Additionally, the context manager involved with keep the caught exception 
in its exc_value member after it has been successfully thrown (and 
matched)
msg89330 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2009-06-13 13:17
This was suggested before on Python-dev and Guido rejected it as it is
an 'odd' API for a unittest assert method - none of the others return
anything.
msg89351 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2009-06-14 16:39
Guido's comment is out of date.  The assertRaises(exception) already 
returns something:  A context manager.

If we don't want to return the object, how about retainig it in the 
context manager, then?
It is very awkward otherwise to make any kinds of assertions on the thrown 
exceptions.  Using the Regexp variant is rather half-assed.
msg89352 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2009-06-14 16:44
Well, his comment was made after assertRaises had already been made a
context manager. 

Keeping the exception attached to the context manager is an interesting
suggestion and a less 'surprising' change to the API.
msg89353 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2009-06-14 16:45
I disagree that the regex version is half-assed though. If all you want
to do is to make assertions about the exception message (the most common
use case in *my* experience) it is enormously convenient.
msg89356 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2009-06-14 16:52
Uploading a slimmed down patch, with only the exc_value memeber added to 
the assertRaises context manager.
msg89367 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2009-06-14 19:59
Ok, maybe a bad choice of words :)
I don't write unittests that much, of course, but I have found that when I 
am writing tests for stuff such as http, I want to verify the actual error 
code, i.e. HTTPError.code, which is not possible using the message alone.
Same goes for e.g. socket.error
msg89524 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2009-06-19 16:22
I just want to second Kristján's position.  I've used assertRaises a lot
over the years (an implementation in a third-party unit testing library)
and it is extremely common that I want the exception object to perform
the kind of checks he is describing.
msg92018 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2009-08-27 22:21
Committed this much more harmless patch to the trunk as revision 74556
msg92035 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2009-08-28 12:03
Cool.
History
Date User Action Args
2022-04-11 14:56:50adminsetgithub: 50524
2009-08-28 12:03:10michael.foordsetkeywords: patch, patch, easy, needs review

messages: + msg92035
2009-08-27 22:21:32kristjan.jonssonsetstatus: open -> closed
keywords: patch, patch, easy, needs review
messages: + msg92018
2009-07-29 19:23:45draghuramsetkeywords: patch, patch, easy, needs review
nosy: + draghuram
2009-06-19 16:22:43exarkunsetnosy: + exarkun
messages: + msg89524
2009-06-14 19:59:34kristjan.jonssonsetkeywords: patch, patch, easy, needs review

messages: + msg89367
2009-06-14 16:52:33kristjan.jonssonsetkeywords: patch, patch, easy, needs review
files: + unitest2.patch
messages: + msg89356
2009-06-14 16:45:27michael.foordsetkeywords: patch, patch, easy, needs review

messages: + msg89353
2009-06-14 16:44:12michael.foordsetkeywords: patch, patch, easy, needs review

messages: + msg89352
2009-06-14 16:39:46kristjan.jonssonsetstatus: closed -> open
keywords: patch, patch, easy, needs review
messages: + msg89351
2009-06-13 13:17:18michael.foordsetstatus: open -> closed
keywords: patch, patch, easy, needs review
resolution: rejected
messages: + msg89330
2009-06-12 21:45:50georg.brandlsetkeywords: patch, patch, easy, needs review
assignee: michael.foord

nosy: + michael.foord
2009-06-12 21:43:13kristjan.jonssoncreate