<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#ffffff">
On 06/08/2010 15:41, Jorge Vargas wrote:
<blockquote
 cite="mid:AANLkTinP7DNXw4pYtAM+p=4Hu3jYyD2pTY7DyzePQw=x@mail.gmail.com"
 type="cite">On Fri, Aug 6, 2010 at 5:32 AM, Jonathan Lange <span
 dir="ltr">&lt;<a moz-do-not-send="true" href="mailto:jml@mumak.net">jml@mumak.net</a>&gt;</span>
wrote:<br>
  <div class="gmail_quote">
  <blockquote class="gmail_quote"
 style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
    <div>
    <div class="h5">On Fri, Aug 6, 2010 at 4:34 AM, Jorge Vargas &lt;<a
 moz-do-not-send="true" href="mailto:jorge.vargas@gmail.com">jorge.vargas@gmail.com</a>&gt;
wrote:<br>
&gt; Hello,<br>
&gt; Doing this seems really simple. All we need is something like<br>
&gt; def mock_exit(exit_status):<br>
&gt; &nbsp;&nbsp; &nbsp;return exit_status<br>
&gt; class RunCommand(TestCase):<br>
&gt; &nbsp;&nbsp; &nbsp;def setUp(self):<br>
&gt; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;#we setup a fake sys.exit() to allow tests to continue<br>
&gt; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;sys.exit = mock_exit<br>
&gt; However I have two questions.<br>
&gt; 1- do I need a tearDown that will replace sys.exit with the
original<br>
&gt; function?<br>
&gt; 2- if so which is the best way to do this? store it in
self.orig_exit ?<br>
&gt; Or the test collector is smart enough to know it has to wipe out
and<br>
&gt; reimport everything on each testcase ?<br>
&gt; In case someone asks this is with unittest2.<br>
    <br>
    <br>
    </div>
    </div>
Better than tearDown:<br>
    <br>
&nbsp;def patch(self, obj, name, value):<br>
&nbsp; &nbsp;current = getattr(obj, name)<br>
&nbsp; &nbsp;setattr(obj, name, value)<br>
&nbsp; &nbsp;self.addCleanup(setattr, obj, name, current)<br>
&nbsp; &nbsp;return current<br>
    <br>
&nbsp;def setUp(self):<br>
&nbsp; &nbsp;self.patch(sys, 'exit', mock_exit)<br>
    <br>
That way, the code in setUp reads much closer to what you actually
intend.<br>
  </blockquote>
  <div><br>
  </div>
  <div>Awesome. That looks like a great and clean solution. Thank you!</div>
  </div>
</blockquote>
<br>
Another alternative (although Jonathan's is fine) using the mock module.<br>
<br>
from mock import patch<br>
<br>
class SomeTest(TestCase):<br>
<br>
&nbsp;&nbsp;&nbsp; @patch('sys.exit')<br>
&nbsp;&nbsp;&nbsp; def testMethod(self, mock_exit):<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .... # code<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; mock_exit.assert_called_with(return_code)<br>
<br>
<br>
the mock.patch decorator handles patching and unpatching for you and
passes the mock it creates into your test method. The mock has a whole
host of useful features - including the assert_called_with used in the
example above.<br>
<br>
<a class="moz-txt-link-freetext" href="http://pypi.python.org/pypi/mock/">http://pypi.python.org/pypi/mock/</a><br>
<br>
All the best,<br>
<br>
Michael Foord<br>
<blockquote
 cite="mid:AANLkTinP7DNXw4pYtAM+p=4Hu3jYyD2pTY7DyzePQw=x@mail.gmail.com"
 type="cite">
  <pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
testing-in-python mailing list
<a class="moz-txt-link-abbreviated" href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a>
<a class="moz-txt-link-freetext" href="http://lists.idyll.org/listinfo/testing-in-python">http://lists.idyll.org/listinfo/testing-in-python</a>
  </pre>
</blockquote>
<br>
<br>
<pre class="moz-signature" cols="72">-- 
<a class="moz-txt-link-freetext" href="http://www.ironpythoninaction.com/">http://www.ironpythoninaction.com/</a>
<a class="moz-txt-link-freetext" href="http://www.voidspace.org.uk/blog">http://www.voidspace.org.uk/blog</a>

READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies (&#8221;BOGUS AGREEMENTS&#8221;) that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.

</pre>
</body>
</html>