<div id="edo-message"><div>TBH you're completely right. Every time I see someone using unittest andItsHorriblyUnpythonicNames, I want to kill a camel.</div><div><br></div><div>Sometimes, though, I feel like part of the struggle is the alternative. If you dislike unittest, but pytest is too "magical" for you, what do you use? Many<span style="font-size:inherit"> Python testing tools like nose are just test *runners*, so you still need something else. In the end, many just end up back at unittest, maybe with nose on top.</span></div><div><br></div><div>As much as I hate JavaScript, their testing libraries are leagues above what Python has.</div><div><br><div id="edo-signature"><pre>--
Ryan (ライアン)
Yoko Shimomura, ryo (supercell/EGOIST), Hiroyuki Sawano >> everyone else
<a href="http://refi64.com">http://refi64.com</a></pre></div></div></div><div id="edo-original"><div><blockquote type="cite"><div id="edo-meta">On Aug 22, 2017 at 5:09 PM, <<a href="mailto:chris.barker@noaa.gov">Chris Barker</a>> wrote: <br><br></div><div dir="ltr">** Caution: cranky curmudgeonly opinionated comment ahead: **<div><br></div><div><br></div><div>unitest is such an ugly Java-esque static mess of an API that there's really no point in trying to clean it up and make it more pythonic -- go off and use pytest and be happier.</div><div><br></div><div>-CHB</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Aug 22, 2017 at 5:42 AM, Nick Coghlan <span dir="ltr"><<a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 22 August 2017 at 15:34, Nick Coghlan <<a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a>> wrote:<br>
> On 21 August 2017 at 11:32, Neil Girdhar <<a href="mailto:mistersheik@gmail.com">mistersheik@gmail.com</a>> wrote:<br>
>> This question describes an example of the problem:<br>
>> <a href="https://stackoverflow.com/questions/8416208/in-python-is-there-a-good-idiom-for-using-context-managers-in-setup-teardown" rel="noreferrer" target="_blank">https://stackoverflow.com/<wbr>questions/8416208/in-python-<wbr>is-there-a-good-idiom-for-<wbr>using-context-managers-in-<wbr>setup-teardown</a>.<br>
>> You want to invoke a context manager in your setup/tearing-down, but the<br>
>> easiest way to do that is to override run, which seems ugly.<br>
><br>
> Using context managers when you can't use a with statement is one of<br>
> the main use cases for contextlib.ExitStack():<br>
><br>
> def setUp(self):<br>
> self._resource_stack = stack = contextlib.ExitStack()<br>
> self._resource = stack.enter_context(<wbr>MyResource())<br>
><br>
> def tearDown(self):<br>
> self._resource_stack.close()<br>
><br>
> I posted that as an additional answer to the question:<br>
> <a href="https://stackoverflow.com/questions/8416208/in-python-is-there-a-good-idiom-for-using-context-managers-in-setup-teardown/45809502#45809502" rel="noreferrer" target="_blank">https://stackoverflow.com/<wbr>questions/8416208/in-python-<wbr>is-there-a-good-idiom-for-<wbr>using-context-managers-in-<wbr>setup-teardown/45809502#<wbr>45809502</a><br>
<br>
</span>Sjoerd pointed out off-list that this doesn't cover the case where<br>
you're acquiring multiple resources and one of the later acquisitions<br>
fails, so I added the ExitStack idiom that covers that case (using<br>
stack.pop_all() as the last operation in a with statement):<br>
<br>
def setUp(self):<br>
with contextlib.ExitStack() as stack:<br>
self._resource1 = stack.enter_context(<wbr>GetResource())<br>
self._resource2 = stack.enter_context(<wbr>GetOtherResource())<br>
# Failures before here -> immediate cleanup<br>
self.addCleanup(stack.pop_all(<wbr>).close)<br>
# Now cleanup won't happen until the cleanup functions run<br>
<br>
I also remember that using addCleanup lets you avoid defining tearDown entirely.<br>
<span class="im HOEnZb"><br>
Cheers,<br>
Nick.<br>
<br>
--<br>
Nick Coghlan | <a href="mailto:ncoghlan@gmail.com">ncoghlan@gmail.com</a> | Brisbane, Australia<br>
</span><div class="HOEnZb"><div class="h5">______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R (206) 526-6959 voice<br>7600 Sand Point Way NE (206) 526-6329 fax<br>Seattle, WA 98115 (206) 526-6317 main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div>
_______________________________________________
Python-ideas mailing list
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a>
<a href="https://mail.python.org/mailman/listinfo/python-ideas">https://mail.python.org/mailman/listinfo/python-ideas</a>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/">http://python.org/psf/codeofconduct/</a>
</blockquote></div></div>