On Fri, Aug 6, 2010 at 12:43 AM, Mark Roddy <span dir="ltr">&lt;<a href="mailto:markroddy@gmail.com">markroddy@gmail.com</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 Thu, Aug 5, 2010 at 11:34 PM, 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>
</div></div>&gt; _______________________________________________<br>
&gt; testing-in-python mailing list<br>
&gt; <a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
&gt; <a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
&gt;<br>
&gt;<br>
<br>
You have a couple options:<br>
1) Mock sys.exit, but you&#39;re going to want this to be undone if you<br>
monkey patch the sys name space.  Doing so in tearDown() from a saved<br>
attribute is one way.<br></blockquote><div><br></div><div>ok I just wanted to know if unittest2 took care of this by reloading the module. Seems it does not.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

2) A mocking framework will take care of all of this boot strap code<br>
for you automatically.<br></blockquote><div><br></div><div>I know that&#39;s the best option however In this particular project I have a zero-external dependencies (imposed) policy.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

3) Alternatively, you could leverage the fact that sys.exit() raises SystemExit:<br>
def test_calls_sys_exit(self):<br>
    with self.assertRaises(SystemExit) as cm:<br>
        obj.method_that_calls_exit()<br>
    self.assertEqual(cm.exception.code, my_expected_exit_code)<br></blockquote><div><br></div><div>This could work, however I like the mock option a little better.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<font color="#888888"><br>
-Mark<br>
</font></blockquote></div><br>