title: How can I use unittest.mock to remove side effects from code?
body: <p>I have a function with several points of failure:</p>
  
  <pre><code>def setup_foo(creds):
      """
      Creates a foo instance with which we can leverage the Foo virtualization
      platform.
  
      :param creds: A dictionary containing the authorization url, username,
                    password, and version associated with the Foo
                    cluster.
      :type creds:  dict
      """
  
      try:
          foo = Foo(version=creds['VERSION'],
                    username=creds['USERNAME'],
                    password=creds['PASSWORD'],
                    auth_url=creds['AUTH_URL'])
  
          foo.authenticate()
          return foo
      except (OSError, NotFound, ClientException) as e:
          raise UnreachableEndpoint("Couldn't find auth_url {0}".format(creds['AUTH_URL']))
      except Unauthorized as e:
          raise UnauthorizedUser("Wrong username or password.")
      except UnsupportedVersion as e:
          raise Unsupported("We only support Foo API with major version 2")
  </code></pre>
  
  <p>and I'd like to test that all the relevant exceptions are caught (albeit not handled well currently).</p>
  
  <p>I have an initial test case that passes:</p>
  
  <pre><code>def test_setup_foo_failing_auth_url_endpoint_does_not_exist(self):
      dummy_creds = {
          'AUTH_URL' : 'http://bogus.example.com/v2.0',
          'USERNAME' : '', #intentionally blank.
          'PASSWORD' : '', #intentionally blank.
          'VERSION'  : 2 
      }
      with self.assertRaises(UnreachableEndpoint):
          foo = osu.setup_foo(dummy_creds)
  </code></pre>
  
  <p>but how can I make my test framework believe that the AUTH_URL is actually a valid/reachable URL?</p>
  
  <p>I've created a mock class for <code>Foo</code>:</p>
  
  <pre><code>class MockFoo(Foo):
      def __init__(self, *args, **kwargs):
          super().__init__(*args, **kwargs)
  </code></pre>
  
  <p>and my thought is mock the call to <code>setup_foo</code> and remove the side effect of raising an <code>UnreachableEndpoint</code> exception. I know how to <em>add</em> side-effects to a <code>Mock</code> with <code>unittest.mock</code>, but how can I remove them?</p>