检查是否已使用不同的参数多次调用函数 [英] Checking whether function has been called multiple times with different parameters

查看:103
本文介绍了检查是否已使用不同的参数多次调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Assume we have a function f(x,y) and another function

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

我们要编写一个单元测试,以检查是否调用了f的次数并具有正确的参数.

We want to write a Unit test that checks whether f is called the number of times and with the right parameters.

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.

but this only refers to the last call. So assuming g calls f(1,2) and then f(2,3), function.assert_called_with(1,2) is False.

此外,还有

function.call_args_list

产生带有正确参数的call对象列表.将此列表与我们在单元测试中创建的call对象进行比较,感觉很麻烦. call似乎是模拟库的内部类.

which yields a list of call objects with the right parameters. Comparing this list to call object that we create in the unit test feels like a very nasty thing to do. call seems like an internal class of the mock library.

是否有更好的方法可以做到这一点?我使用此设置来测试apply函数的并行执行.

Is there a better way to do this? I use this set-up to test parallel execution of an apply function.

推荐答案

即使@MartinPieters的回答也是正确的,我认为这也不是最好的方法.模拟提供 assert_has_calls 这种职责.

Even @MartinPieters's answer is correct I think that is not the best way to do it. Mock provide assert_has_calls to do this kind of duties.

您的测试可能是:

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

mock.call 是帮助类的地方这些工作.

Where mock.call is a helper class do to these kind of jobs.

请注意,这是一个通话,表示通话列表应在通话列表中,并且不能相等.为了解决这个问题,我通常如下定义自己的助手assert_is_calls()

Pay attention that is a has call and means the call list should be in the list of call and not equal. To solve it I usually define my own helper assert_is_calls() as follow

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
>>> 

这篇关于检查是否已使用不同的参数多次调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆