我希望了解如何从已导入的模块中修补一个函数。
目前我所在的位置是这样的:
app/mocking.py:
from app.my_module import get_user_name
def test_method():
return get_user_name()
if __name__ == "__main__":
print "Starting Program..."
test_method()
应用程序/我的模块/__init__.py:
def get_user_name():
return "Unmocked User"
测试/模拟测试.py:
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()
这并不像我期望的那样工作。 “patched”模块只是返回了get_user_name
未经mock处理的值。我如何mock我导入到测试命名空间中的其他包的方法?