擅长:python、mysql、java
<p>假设您正在使用这个<a href="http://www.voidspace.org.uk/python/mock/index.html" rel="noreferrer">mock</a>库:</p>
<pre><code>def f1():
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() # f2 with changed f1 -> prints (60, True)
</code></pre>
<p>如果您的代码被分成多个模块,您可能需要用模块/函数的路径替换<code>__main__.f1</code>。</p>