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.

Author magnusc
Recipients magnusc
Date 2015-05-06.12:16:57
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1430914617.89.0.683303246144.issue24134@psf.upfronthosting.co.za>
In-reply-to
Content
Hi

I have a little issue with the current assertRaises implementation. It seems that it might produce a little different results depending on how you use it.

self.assertRaises(IOError, None)

will not produce the same result as:

with self.assertRaises(IOError):
    None()

In the first case everything will be fine due to the fact that assertRaises will actually return a context if the second callable parameters is None. The second case will throw a TypeError 'NoneType' object is not callable.

I don't use None directly, but replace it with a variable of unknown state and you get a little hole where problems can creep in. In my case I was testing function decorators and that they should raise some exceptions on special cases. It turned our that I forgot to return the decorator and instead got the default None. But my tests didn't warn me about that.

Bottom line is that if I use the first assertRaises(Exception, callable) I can't rely on it to check that the callable is actually something callable.

I do see that there is a benefit of the context way, but in my opinion current implementation will allow problems to go undetected. 

My solution to this would be to rename the context variant into something different:
with self.assertRaisesContext(Exception):
    do_something()

A side note on this is that reverting back to the original behavior would allow you to reevaluate issue9587 for returning the actual exception.
History
Date User Action Args
2015-05-06 12:16:57magnuscsetrecipients: + magnusc
2015-05-06 12:16:57magnuscsetmessageid: <1430914617.89.0.683303246144.issue24134@psf.upfronthosting.co.za>
2015-05-06 12:16:57magnusclinkissue24134 messages
2015-05-06 12:16:57magnusccreate