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.
Created on 2010年08月13日 14:57 by denversc, last changed 2022年04月11日 14:57 by admin. This issue is now closed.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| unittest.assertRaises.returnex.v1.patch | denversc, 2010年08月13日 14:57 | Proposed Patch | ||
| Messages (7) | |||
|---|---|---|---|
| msg113781 - (view) | Author: Denver Coneybeare (denversc) * | Date: 2010年08月13日 14:57 | |
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! |
|||
| msg113810 - (view) | Author: Michael Foord (michael.foord) * (Python committer) | Date: 2010年08月13日 18:45 | |
If you want the exception then use assertRaises in a with statement. The exception is available as an attribute on the context manager. |
|||
| msg113812 - (view) | Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) | Date: 2010年08月13日 18:56 | |
That was a bit abrupt, want't it? Denver is well aware of this and said: "but sometimes using the context manager approach adds unnecessary bloat to already long-winded unit tests." I happen to agree with him and don't see why we can't discuss this some. We never did discuss this fully in issue 6275, I just tempered my patch a bit to at least get something done. I don't think this should be rejected out of hand simply because TBDFL said that its an "'odd' API for a unittest assert method". assertRaises already return a context manager if called without a callable (an odd api?), and it can just as well return an exception if called _with_ a callable. |
|||
| msg113813 - (view) | Author: Michael Foord (michael.foord) * (Python committer) | Date: 2010年08月13日 18:57 | |
Sorry, it's a reopened bug requesting a feature that has already been considered and rejected previously. Yes I was abrupt, my apologies - I'm trying to clear my backlog before I go away. |
|||
| msg113994 - (view) | Author: Denver Coneybeare (denversc) * | Date: 2010年08月15日 18:53 | |
Michael: Do you disagree with assertRaises() returning the exception object on principle? Or is this just the consensus that you got from the mailing list, including Guido's comment. My particular use case is that I want to check certain attributes being set on the raised exception and I feel that the context manager approach is overkill for my tests since it's just one method call in the context manager. I don't understand why it is considered "odd" for assertRaises() to return the result for further inspection... I need to get it some way and assertRaises() has a reference to it. Thanks for considering this request further. |
|||
| msg114015 - (view) | Author: Michael Foord (michael.foord) * (Python committer) | Date: 2010年08月15日 22:16 | |
Providing access to the exception on the context manager was *precisely* to meet the use case of wanting to make assertions about the exception. I tend to agree with Guido that having one of the asserts return something is a bit odd, but irrespective of that I don't think we should have two ways of doing exactly the same thing. In general I find that the with statement version of assertRaises looks a lot better than the old way of calling it, so I guess I also disagree that it is "overkill" or adds bloat. Sorry guys. |
|||
| msg114041 - (view) | Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) | Date: 2010年08月16日 09:28 | |
Pehaps it hasn't been demonstrated before, but just for the sake of argument (and because I'm a persistant bugger), here are the two different cases: current: ctxt = self.assertRaises(MyException) with ctxt: foo() self.assertEqual(ctxt.exception.value, 1) suggested: e = self.assertRaises(MyExcetpion, foo) self.assertEqual(e.value, 1) The inconvenient bit about the current method is having to keep the context manager around. Also note that the current way of looking at the exception object makes it blatantly clear that self.assertRaises() is returning an object. That, imho, breaks the argument about self.assert* methods not returning any info. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:05 | admin | set | github: 53796 |
| 2016年09月14日 14:44:34 | r.david.murray | link | issue28135 superseder |
| 2015年01月13日 00:38:20 | martin.panter | set | nosy:
+ martin.panter |
| 2010年11月02日 22:06:13 | eric.araujo | set | nosy:
+ eric.araujo |
| 2010年08月16日 09:28:24 | kristjan.jonsson | set | messages: + msg114041 |
| 2010年08月15日 22:16:37 | michael.foord | set | messages: + msg114015 |
| 2010年08月15日 18:53:37 | denversc | set | messages: + msg113994 |
| 2010年08月13日 18:57:28 | michael.foord | set | messages: + msg113813 |
| 2010年08月13日 18:56:01 | kristjan.jonsson | set | messages: + msg113812 |
| 2010年08月13日 18:45:01 | michael.foord | set | status: open -> closed resolution: rejected messages: + msg113810 stage: resolved |
| 2010年08月13日 15:27:38 | pitrou | set | assignee: michael.foord |
| 2010年08月13日 14:57:42 | denversc | create | |