【发布时间】:2021-04-21 14:39:35
【问题描述】:
您好,我正在寻找一种方法来模拟由函数调用的函数。 目前我只找到类似这样的例子:
def f1():
a = 'do not make this when f1() is mocked'
return 10, True
def f2():
num, stat = f1()
return 2*num, stat
import mock
print f2() # Unchanged f1 -> prints (20, True)
with mock.patch('__main__.f1') as MockClass: # replace f1 with MockClass
MockClass.return_value = (30, True) # change the return value
print f2() # prints now 60,
这样做的问题是方法的返回值被覆盖了,但函数的实际逻辑仍然被执行。 函数中发生的事情应该被忽略。 换句话说,我想为测试重载函数。 这是个好主意,还是有其他解决问题的方法?
【问题讨论】:
标签: python mocking python-unittest patch