unittest: assertRaises() with an instance instead of a type
Ulrich Eckhardt
ulrich.eckhardt at dominolaser.com
Thu Mar 29 03:28:15 EDT 2012
Am 28.03.2012 20:26, schrieb Terry Reedy:
> On 3/28/2012 8:28 AM, Ulrich Eckhardt wrote:
>> with self.assertRaises(MyException(SOME_FOO_ERROR)):
>> foo()
>
> I presume that if this worked the way you want, all attributes would
> have to match. The message part of builtin exceptions is allowed to
> change, so hard-coding an exact expected message makes tests fragile.
> This is a problem with doctest.
I would have assumed that comparing two exceptions leaves out messages
that are intended for the user, not as part of the API. However, my
expectations aren't met anyway, because ...
>> This of course requires the exception to be equality-comparable.
>
> Equality comparison is by id. So this code will not do what you want.
>>> Exception('foo') == Exception('foo')
False
Yikes! That was unexpected and completely changes my idea. Any clue
whether this is intentional? Is identity the fallback when no equality
is defined for two objects?
Thanks for your feedback!
Uli
More information about the Python-list
mailing list