当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《检查是否已使用不同的参数多次调用函数》经验,为你挑选了1个好方法。

假设我们有一个功能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功能的并行执行。



1> Michele d'Am..:

甚至@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)

>>> f(2)

>>> 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 "", line 1, in 
  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)

>>> 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 "", line 1, in 
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 "", line 1, in 
  File "", 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 "", line 1, in 
  File "", line 2, in assert_is_calls
AssertionError
>>> 

推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有