title: Python unittest mock: Is it possible to mock the value of a method's default arguments at test time?
body: <p>I have a method that accepts default arguments:</p>
  
  <pre><code>def build_url(endpoint, host=settings.DEFAULT_HOST):
      return '{}{}'.format(host, endpoint)
  </code></pre>
  
  <p>I have a test case that exercises this method:</p>
  
  <pre><code>class BuildUrlTestCase(TestCase):
      def test_build_url(self):
          """ If host and endpoint are supplied result should be 'host/endpoint' """
  
          result = build_url('/end', 'host')
          expected = 'host/end'
  
          self.assertEqual(result,expected)
  
       @patch('myapp.settings')
       def test_build_url_with_default(self, mock_settings):
          """ If only endpoint is supplied should default to settings"""
          mock_settings.DEFAULT_HOST = 'domain'
  
          result = build_url('/end')
          expected = 'domain/end'
  
          self.assertEqual(result,expected)
  </code></pre>
  
  <p>If I drop a debug point in <code>build_url</code> and inspect this attribute <code>settings.DEFAULT_HOST</code> returns the mocked value. However the test continues to fail and the assertion indicates <code>host</code> is assigned the value from my actual <code>settings.py</code>. I know this is because the <code>host</code> keyword argument is set at import time and my mock is not considered.</p>
  
  <p><strong>debugger</strong></p>
  
  <pre><code>(Pdb) settings
  &lt;MagicMock name='settings' id='85761744'&gt;                                                                                                                                                                                               
  (Pdb) settings.DEFAULT_HOST
  'domain'
  (Pdb) host
  'host-from-settings.com'                                                                                                                                                 
  </code></pre>
  
  <p>Is there a way to override this value at test time so that I can exercise the default path with a mocked <code>settings</code> object?</p>