Gia*_*nis 2 python unit-testing mocking python-unittest python-3.4
我有以下目录
/root
/app
/api
my_api.py
/service
my_service.py
/tests
test_api.py
Run Code Online (Sandbox Code Playgroud)
my_api.py
import app
def run_service():
app.service.my_service.service_function()
Run Code Online (Sandbox Code Playgroud)
test_api.py
@patch('app.service.my_service.service_function')
test_run_service(self,mock_service):
mock_service.return_value = 'Mock'
response = self.client.get(url_for('api.run_service')
self.assertTrue(response == expected_responce)
Run Code Online (Sandbox Code Playgroud)
以上工作.我无法弄清楚,我需要修补哪个模块,以防我想service_function在my_apy.py中导入,如下所示:
from app.service.my_service import service_function
Run Code Online (Sandbox Code Playgroud)
如果我像上面那样进行导入,模拟停止工作.
您需要修补app.api.my_api.service_function,因为这是已绑定到导入对象的全局名称:
@patch('app.api.my_api.service_function')
test_run_service(self, mock_service):
mock_service.return_value = 'Mock'
response = self.client.get(url_for('api.run_service')
self.assertTrue(response == expected_responce)
Run Code Online (Sandbox Code Playgroud)
请参阅补丁部分:
基本原则是修改查找对象的位置,该位置不一定与定义对象的位置相同.
| 归档时间: |
|
| 查看次数: |
40 次 |
| 最近记录: |