我必须对遍布各处且难以编写测试的服务进行一些更改。我想运行一堆已知请求并转储一些底层调用(以便稍后可以通过一些mock.return_value
反馈它们)。很明显,我可以在运行中临时抛出一些转储以获取所需的数据,但是我想知道是否可以通过mock
或类似的工具从外部实用程序脚本中获取数据。像这样:
import mock
from main_stuff import some_function # function that uses aClass
from a import aClass
@mock.patch("a.aClass")
def test(mock_a):
a_inst = aClass()
mock_a.return_value = a_inst
with mock.patch.object(a_inst, "a_method", wraps=a_inst.a_method) as mock_method:
result = some_function()
# Want the result of a_inst.a_method when it was called during some_function.