我有一个正在测试的系统 B,它使用一些 A 和一些我想模拟的昂贵功能,但 B 依赖于获取 A 的多个实例。
http://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch说:“如果类被实例化多次,你可以使用 side_effect 每次返回一个新的模拟。” 但我不明白,如何:(
我将如何更改此示例:
import time
class A:
def say_hi(self):
print("lets do something horribly expensive")
time.sleep(2)
class B:
def __init__(self):
self.a_map = {}
def get_a(self):
a = A()
a.say_hi()
self.a_map[a] = True
with patch('__main__.A') as mockA:
b = B()
def side_effect():
# return DEFAULT # no success
return mockA # no success neither
# return mockA.clone() # :(
mockA.side_effect = side_effect # no help
# mockA.side_effect = [DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT] # nope
# mockA.side_effect = [mockA, mockA, mockA, mockA, mockA] # nope
# mockA.side_effect = [DEFAULT, mockA, mockA, mockA, mockA] # nope, but hey! I get 2 instances in my key set now!
# mockA.side_effect = [DEFAULT, mockA, mockA.clone(), mockA.clone().clone(), mockA.clone().clone().clone()] # would work, but hey, seriously?
for _ in range(5):
b.get_a()
print(b.a_map)
assert len(b.a_map) == 5
产生这个输出:
{<MagicMock name='A' id='139998588645520'>: True}
Traceback (most recent call last):
File "<stdin>", line 11, in <module>
AssertionError
(不模拟断言成功,但需要 10 秒。)