简短的回答是不,你不能使用patch.multiple() 来做到这一点。如patch.multiple 中所述,所有参数都将应用于所有创建的模拟,并且所有参数必须是同一对象的属性。您必须一次通过单个补丁调用来执行此操作。
不幸的是,您使用的是 python 2.6,因此您只能使用 nested 前 contextlib 就像在 python: create a "with" block on several context managers 和 Multiple context `with` statement in Python 2.6 中指出的那样。
也许更清洁和简单的方法是使用@patch作为装饰器:
@patch("foo.load",side_effects=["a","b","c"])
@patch("bar.check",return_value=True)
def test_mytest(mock_check,mock_load):
take_action()
assert mock_load.called
assert mock_check.called
如果您在测试类的所有测试中都需要它,您可以装饰该类并在所有测试方法中使用模拟:
@patch("foo.load",side_effects=["a","b","c"])
@patch("bar.check",return_value=True)
class TestMyTest(unittest.TestCase)
def test_mytestA(self,mock_check,mock_load):
take_action()
self.assertTrue(mock_load.called)
self.assertTrue(mock_check.called)
def test_mytestA(self,mock_check,mock_load):
mock_check.return_value = False
take_action()
self.assertTrue(mock_load.called)
self.assertTrue(mock_check.called)
最后你可以使用with 和contextlib 来做到这一点,第一个例子变成:
from contextlib import nested
with nested(patch("foo.load",side_effects=["a","b","c"]), patch("bar.check",return_value=True)) as (mock_load, mock_check):
take_action()
assert mock_load.called
assert mock_check.called
...或者手动嵌套....
with patch("foo.load",side_effects=["a","b","c"]) as mock_load:
with patch("bar.check",return_value=True)) as mock_check:
take_action()
assert mock_load.called
assert mock_check.called
我的感觉是装饰器是最易读和最容易使用的。