检查是否已使用不同参数多次调用函数

时间:2015-12-11 14:21:59

标签: python unit-testing testing mocking

假设我们有一个函数f(x,y)和另一个函数

def g():
       # ...
       f(i,j) # i,j vary and f is called multiple times
       # ...

我们想编写一个单元测试,用于检查f是否被调用次数并使用正确的参数。

def test_g():
      with patch('mymodule.f') as function:
          assert function.gacs.call_count == correct_no_calls

function.assert_called_with(...)

但这只是指最后一次通话。因此,假设g调用f(1,2)然后调用f(2,3),则function.assert_called_with(1,2)False

此外,还有

function.call_args_list

生成具有正确参数的call个对象列表。将此列表与我们在单元测试中创建的call对象进行比较,感觉就像是一件非常讨厌的事情。 call似乎是模拟库的内部类。

有更好的方法吗?我使用此设置来测试apply函数的并行执行。

2 个答案:

答案 0 :(得分:4)

即使@ MartinPieters的答案是正确的我认为这不是最好的方法。模拟提供assert_has_calls来履行这种职责。

您的测试可能是:

function.assert_has_calls([mock.call(1, 2), mock.call(2, 3)])

mock.call是帮助者类对这些工作做的事情。

注意电话,这意味着呼叫列表应该在呼叫列表中并且不等于。要解决这个问题,我通常会定义自己的帮助器assert_is_calls(),如下所示

def assert_is_calls(m, calls, any_order=False):
   assert len(m.mock_calls) == len(calls)
   m.assert_has_calls(calls, any_order=any_order)

那是一个简历的例子

>>> import mock
>>> f = mock.Mock()
>>> f(1)
<Mock name='mock()' id='139836302999952'>
>>> f(2)
<Mock name='mock()' id='139836302999952'>
>>> f.assert_has_calls([mock.call(1), mock.call(2)])
>>> f.assert_has_calls([mock.call(2), mock.call(1)])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/damico/.local/lib/python2.7/site-packages/mock/mock.py", line 969, in assert_has_calls
    ), cause)
  File "/home/damico/.local/lib/python2.7/site-packages/six.py", line 718, in raise_from
    raise value
AssertionError: Calls not found.
Expected: [call(2), call(1)]
Actual: [call(1), call(2)]
>>> f.assert_has_calls([mock.call(2), mock.call(1)], any_order=True)
>>> f(3)
<Mock name='mock()' id='139836302999952'>
>>> f.assert_has_calls([mock.call(2), mock.call(1)], any_order=True)
>>> f.assert_has_calls([mock.call(1), mock.call(2)])
>>> assert len(f.mock_calls)==2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>> assert len(f.mock_calls)==3
>>> def assert_is_calls(m, calls, any_order=False):
...    assert len(m.mock_calls) == len(calls)
...    m.assert_has_calls(calls, any_order=any_order)
... 
>>> assert_is_calls(f, [mock.call(1), mock.call(2), mock.call(3)])
>>> assert_is_calls(f, [mock.call(1), mock.call(3), mock.call(2)])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in assert_is_calls
  File "/home/damico/.local/lib/python2.7/site-packages/mock/mock.py", line 969, in assert_has_calls
    ), cause)
  File "/home/damico/.local/lib/python2.7/site-packages/six.py", line 718, in raise_from
    raise value
AssertionError: Calls not found.
Expected: [call(1), call(3), call(2)]
Actual: [call(1), call(2), call(3)]
>>> assert_is_calls(f, [mock.call(1), mock.call(3), mock.call(2)], True)
>>> assert_is_calls(f, [mock.call(1), mock.call(3)], True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in assert_is_calls
AssertionError
>>> 

答案 1 :(得分:3)

测试Mock().mock_calls list是否等于您提供的mock.call() objects列表:

self.assertEquals(function.mock_calls, [
    mock.call(1, 2),
    mock.call(2, 3),
])

这为您提供了精确控制,同时要求匹配的顺序和呼叫数量。

mock.call()类不是内部的,它可以用于这样的断言。