擅长:python、mysql、java
<p>所以,经过更多的研究和反复试验,我用这里的大多数例子:<a href="http://www.voidspace.org.uk/python/mock/patch.html" rel="nofollow noreferrer">http://www.voidspace.org.uk/python/mock/patch.html</a>,解决了我的问题。</p>
<pre><code>import mock
import os
def side_effect(arg):
if arg == 1:
return True
else:
return False
class TestClass(unittest.TestCase):
patcher = mock.patch('os.path.exists')
mock_thing = patcher.start()
mock_thing.side_effect = side_effect
arg_list = [1, 2]
ret = test(arg_list)
self.assertItemsEqual([1], ret)
</code></pre>
<p><code>test</code>为<code>arg_list</code>中的每个项调用<code>os.path.exist</code>,并返回<code>os.path.exist</code>为其返回<code>True</code>的所有项的列表。这个测试现在通过了我的要求。</p>