模拟从输入参数派生的函数的 return 值

Mock the return value of a function derived from an input parameter

我想模拟从输入参数 (a) 派生的函数的 return 值。
这是我的代码。

 def load_data(a, b, c):
     data_source = a.get_function(b, c)
     ...
     return r_1, r_2, r_3

这是我尝试过但没有奏效的方法。我找不到任何模拟此类函数的 return 的来源。

@classmethod
def setUpClass(cls):
    cls.a = A.create()
    cls.data_needed_return = read_file()

def test_function(self):
    mocked = mock.Mock()
    self.a.get_function.return_value = self.data_needed_return

    import python_file
    data_source = python_file.load_data(None, None, None)

有人可以帮忙吗?

不确定我是否正确理解了您的问题,但您是否在寻找类似以下的内容?


from unittest.mock import Mock

def load_data(a, b, c):
    data_source = a.get_function(b, c)
    return data_source

a = Mock()
a.get_function.return_value = "Some data"

x = load_data(a, 2, 3)

print(x)