When you are using the patch
decorator from the unittest.mock
package you are patching it in the namespace that is under test (in this case app.mocking.get_user_name
), not the namespace the function is imported from (in this case app.my_module.get_user_name
).
To do what you describe with @patch
try something like the below:
from mock import patch
from app.mocking import test_method
class MockingTestTestCase(unittest.TestCase):
@patch('app.mocking.get_user_name')
def test_mock_stubs(self, test_patch):
test_patch.return_value="Mocked This Silly"
ret = test_method()
self.assertEqual(ret, 'Mocked This Silly')
The standard library documentation includes a useful section describing this.