<div dir="ltr">Loops in test methods are somewhat precarious things. 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, but often there's a more clear way to eliminate the loop.<div>
<br></div><div style>That being said, assertRaises in Py3 takes a msg keyword arg which could be used to show the usual message (your x here). I presume you're not using it (by virtue of the fact that you're using unittest2), and it appears that msg hasn't been backported to unittest2. Py2 doesn't have keyword-only args strictly speaking, but it could be done by plucking msg out of kwargs, albeit that'd break some backwards compat. Not sure why that hasn't been done, or if it was intentionally left out.</div>
<div style><br></div><div style>In the absence of that, I'd recommend using the simple thing:</div><div style><br></div><div style>for i in stuff:</div><div style> try:</div><div style> whatever(i)</div><div style>
except ThingError:</div><div style> pass</div><div style> else:</div><div style> self.fail("Expected ThingError for %r" % x)</div><div style><br></div><div style>Salt as needed.</div><div style>
<br></div><div style>Cheers,</div><div style><br></div><div style>Julian</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Jan 13, 2013 at 10:40 PM, Olemis Lang <span dir="ltr"><<a href="mailto:olemis@gmail.com" target="_blank">olemis@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all !<br>
:)<br>
<br>
The body of a `unittest2` test case looks like this ...<br>
<br>
{{{<br>
#!python<br>
<br>
# ------8<--- Further code omitted ---8<------<br>
for x in seq:<br>
# ------8<--- Further code omitted ---8<------<br>
with self.assertRaises(MyException) as cm:<br>
# Assertions et al.<br>
<br>
}}}<br>
<br>
As you can see there's a chance for assertRaises to fail and as a<br>
consequence a message like «AssertionError: MyException not raised» is<br>
included in ouput . That's not informative enough *in this case* since<br>
I'd like to know e.g. the exact value of x .<br>
<br>
Q:<br>
- Is there a chance to customize `assertRaises` failure message ?<br>
- Is there any chance for e.g. dumping `locals()` ?<br>
- Can `unittest2` be helpful somehow ?<br>
- Any other suggestions ?<br>
<br>
Any feedback will be welcome . Thanks in advance .<br>
<br>
<br>
--<br>
Regards,<br>
<br>
Olemis.<br>
<br>
Blog ES: <a href="http://simelo-es.blogspot.com/" target="_blank">http://simelo-es.blogspot.com/</a><br>
Blog EN: <a href="http://simelo-en.blogspot.com/" target="_blank">http://simelo-en.blogspot.com/</a><br>
<br>
Featured article:<br>
<br>
_______________________________________________<br>
testing-in-python mailing list<br>
<a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
<a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
</blockquote></div><br></div>