[TIP] unittest2 assertRaises failure message

Olemis Lang olemis at gmail.com
Tue Jan 15 07:31:37 PST 2013


On 1/13/13, Julian Berman <julian at grayvines.com> wrote:
> Loops in test methods are somewhat precarious things.

.. not if things get a bit meta ... ;)

> If something
> unexpected happens (like your sequence being unexpectedly empty, say),
> you've now got quite the false sense of security. Guard assertions can
> help,

understood , but this is not he case

> but often there's a more clear way to eliminate the loop.
>

yes , copy / paste , thus having a very long test case full of duplicated code .

> That being said, assertRaises in Py3 takes a msg keyword arg
>
[...]
> In the absence of that, I'd recommend using the simple thing:
>
> for i in stuff:
>     try:
>         whatever(i)
>     except ThingError:
>         pass
>     else:
>         self.fail("Expected ThingError for %r" % x)
>

jftr , I came up with a different solution available in this patch
[1]_ . I don't know whether it's worthy to include something like that
upstream in unittest2 , but should be generic enough for other people
requiring a similar solution . It retrieves test case locals which are
supplied in for string interpolation with
test_case.exceptFailureMessage

.. [1] Patch: BH Multiproduct #115 : Test cases improved. Logging
capabilities in assertRaises
        (https://issues.apache.org/bloodhound/attachment/ticket/115/t115_r1433322_unittest2_assertRaises_msg.diff)

Regards,

Olemis.

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:



More information about the testing-in-python mailing list