title: Python mock requests.post to throw exception
body: <p>Using Python 3.5, requests==2.18.4, Flask==0.12.2, urllib3==1.22</p>
  
  <p>I have a method <code>some_method</code> in my main <code>server.py</code> file, that is supposed to make a <code>POST</code> to some url with some data:</p>
  
  <pre><code>def some_method(url, data):
      ...
      error = None
      try:
          response = requests.post(url, json=data)
      except requests.exceptions.ConnectionError as e:
          ...
          app.logger.error(...)
          response = None
          error = str(e)
      return error, response
  </code></pre>
  
  <p>The server file defines: <code>app = Flask(__name__)</code>, and <code>some_method</code> is called from <code>@app.route(... methods=['PATCH'])</code>.<br>
  If this method throws an error, the route will eventually return a <code>500</code>.<br></p>
  
  <p>Tests are run from a test file importing the app with <code>import server</code> and <code>app = server.app</code>, using <code>unittest</code>, and importing <code>mock.patch</code>.<br></p>
  
  <p>I am able to test the overall app behavior, with a test that shows that the app route behave as expected when the method returns an error and seeing that the route terminates at the right spot:<br></p>
  
  <pre><code>class ServerTestCase(unittest.TestCase):
      ...
      @patch('server.some_method')
      def test_route_response_status_500_when_throws(self, mock_response):
          mock_response.return_value = 'some_error_string', None
          response = self.app.patch(some_url, some_data, content_type='application/json')
          self.assertEqual(response.status_code, 500)
  </code></pre>
  
  <p>However, I would really like to have another test to test <code>some_method</code> in isolation:</p>
  
  <ol>
  <li>Mock <code>requests.post</code> to throw <code>requests.exceptions.ConnectionError</code></li>
  <li>Show that the method logs an error (I know I can mock my <code>app.logger</code> and assert that it logged during the execution)</li>
  </ol>