On Fri, Aug 6, 2010 at 5:32 AM, Jonathan Lange <span dir="ltr">&lt;<a href="mailto:jml@mumak.net">jml@mumak.net</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On Fri, Aug 6, 2010 at 4:34 AM, Jorge Vargas &lt;<a 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;     return exit_status<br>
&gt; class RunCommand(TestCase):<br>
&gt;     def setUp(self):<br>
&gt;         #we setup a fake sys.exit() to allow tests to continue<br>
&gt;         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>
  def patch(self, obj, name, value):<br>
    current = getattr(obj, name)<br>
    setattr(obj, name, value)<br>
    self.addCleanup(setattr, obj, name, current)<br>
    return current<br>
<br>
  def setUp(self):<br>
    self.patch(sys, &#39;exit&#39;, 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>