[TIP] Mocking sys.exit() and replacing it back?

Michael Foord fuzzyman at voidspace.org.uk
Fri Aug 6 07:51:10 PDT 2010

On 06/08/2010 15:41, Jorge Vargas wrote:
> On Fri, Aug 6, 2010 at 5:32 AM, Jonathan Lange <jml at mumak.net 
> <mailto:jml at mumak.net>> wrote:
>     On Fri, Aug 6, 2010 at 4:34 AM, Jorge Vargas
>     <jorge.vargas at gmail.com <mailto:jorge.vargas at gmail.com>> wrote:
>     > Hello,
>     > Doing this seems really simple. All we need is something like
>     > def mock_exit(exit_status):
>     >     return exit_status
>     > class RunCommand(TestCase):
>     >     def setUp(self):
>     >         #we setup a fake sys.exit() to allow tests to continue
>     >         sys.exit = mock_exit
>     > However I have two questions.
>     > 1- do I need a tearDown that will replace sys.exit with the original
>     > function?
>     > 2- if so which is the best way to do this? store it in
>     self.orig_exit ?
>     > Or the test collector is smart enough to know it has to wipe out and
>     > reimport everything on each testcase ?
>     > In case someone asks this is with unittest2.
>     Better than tearDown:
>      def patch(self, obj, name, value):
>        current = getattr(obj, name)
>        setattr(obj, name, value)
>        self.addCleanup(setattr, obj, name, current)
>        return current
>      def setUp(self):
>        self.patch(sys, 'exit', mock_exit)
>     That way, the code in setUp reads much closer to what you actually
>     intend.
> Awesome. That looks like a great and clean solution. Thank you!

Another alternative (although Jonathan's is fine) using the mock module.

from mock import patch

class SomeTest(TestCase):

     def testMethod(self, mock_exit):
         .... # code

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.


All the best,

Michael Foord
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python


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 ("BOGUS AGREEMENTS") 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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20100806/65e99332/attachment.htm>

More information about the testing-in-python mailing list