title: Python Mocking a function from an imported module
body: <p>I want to understand how to <code>@patch</code> a function from an imported module.</p>
  
  <p>This is where I am so far.</p>
  
  <p><strong>app/mocking.py:</strong></p>
  
  <pre><code>from app.my_module import get_user_name
  
  def test_method():
    return get_user_name()
  
  if __name__ == "__main__":
    print "Starting Program..."
    test_method()
  </code></pre>
  
  <p><strong>app/my_module/__init__.py:</strong></p>
  
  <pre><code>def get_user_name():
    return "Unmocked User"
  </code></pre>
  
  <p><strong>test/mock-test.py:</strong></p>
  
  <pre><code>import unittest
  from app.mocking import test_method 
  
  def mock_get_user():
    return "Mocked This Silly"
  
  @patch('app.my_module.get_user_name')
  class MockingTestTestCase(unittest.TestCase):
  
    def test_mock_stubs(self, mock_method):
      mock_method.return_value = 'Mocked This Silly')
      ret = test_method()
      self.assertEqual(ret, 'Mocked This Silly')
  
  if __name__ == '__main__':
    unittest.main()
  </code></pre>
  
  <p>This does <strong>not</strong> work as I would expect. The "patched" module simply returns the unmocked value of <code>get_user_name</code>. How do I mock methods from other packages that I am importing into a namespace under test?</p>