On Fri, Aug 6, 2010 at 5:32 AM, Jonathan Lange <span dir="ltr"><<a href="mailto:jml@mumak.net">jml@mumak.net</a>></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 <<a href="mailto:jorge.vargas@gmail.com">jorge.vargas@gmail.com</a>> wrote:<br>
> Hello,<br>
> Doing this seems really simple. All we need is something like<br>
> def mock_exit(exit_status):<br>
> return exit_status<br>
> class RunCommand(TestCase):<br>
> def setUp(self):<br>
> #we setup a fake sys.exit() to allow tests to continue<br>
> sys.exit = mock_exit<br>
> However I have two questions.<br>
> 1- do I need a tearDown that will replace sys.exit with the original<br>
> function?<br>
> 2- if so which is the best way to do this? store it in self.orig_exit ?<br>
> Or the test collector is smart enough to know it has to wipe out and<br>
> reimport everything on each testcase ?<br>
> 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, '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>