擅长:python、mysql、java
<p>没有直接的方法去做你想做的事。最简单的方法是使用多行<code>patch</code>decorator和{a1}来模拟<code>open</code>:</p>
<pre><code>m = mock.MagicMock(side_effect=OSError('not found'))
mopen = mock.mock_open(m)
@mock.patch('file1.open', mopen, create=True)
@mock.patch('file2.open', mopen, create=True)
def test_case():
with self.assertRaises(OSError):
hello1()
mopen.side_effect = IOError('er')
with self.assertRaises(IOError):
hello2()
</code></pre>