On Fri, Aug 6, 2010 at 12:43 AM, Mark Roddy <span dir="ltr"><<a href="mailto:markroddy@gmail.com">markroddy@gmail.com</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 Thu, Aug 5, 2010 at 11:34 PM, 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>
</div></div>> _______________________________________________<br>
> testing-in-python mailing list<br>
> <a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
> <a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
><br>
><br>
<br>
You have a couple options:<br>
1) Mock sys.exit, but you'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'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>