我的模块engine.py
中有一个util,它是从另一个文件导入的:
from main.utils.string import get_random_string
def generate_random_string():
return get_random_string()
在我的测试文件中:
def test_generate_random_string(mocker):
mocker.patch('main.utils.string.get_random_string', return_value='123456')
但是,它仍然尝试使用string.get_random_string
的真正实现,而不是我创建的模拟,除非我将engine.py
更改为:
from main.utils import string
def generate_random_string():
return string.get_random_string()
如何在不向engine.py
导入整个string
模块的情况下实现模拟部分
发布于 2018-06-11 18:25:29
我已经通过将mocker.patch('main.utils.string.get_random_string', return_value='123456')
更改为mocker.patch('engine.get_random_string', return_value='123456')
成功地实现了这一点。
详情可以在here上找到。
https://stackoverflow.com/questions/50794468
复制相似问题