我有以下代码:
我的模块.py
def my_func(number):
def nested_func(value):
'''
Doing some calculation
'''
return result
output = []
for i in range(number):
res = nested_func(i)
output.append(res)
return output
我在 test_my_module.py 中使用 pytest 和 pytest-mock 和 mocker 作为夹具
test_my_module.py
def test_my_module(mocker):
expected_res = [1, 1, 1]
mocker.patch('nested_func', return_value=1)
from my_module import my_func
assert my_func(3) == expected_res
但是当我在 py.test 中运行时出现错误:
TypeError: Need a valid target to patch. You supplied: 'nested_func'
有没有什么方法可以修改 mocker.patch 函数\方法,它们在测试模块中不可见,并且嵌套在该函数中,我想测试?