我如何检查调用参数是否会随 unittest.mock 改变

How can i check call arguments if they will change with unittest.mock(我如何检查调用参数是否会随 unittest.mock 改变)
本文介绍了我如何检查调用参数是否会随 unittest.mock 改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的一个类在一个列表中累积值,将该列表用作另一个对象的方法的参数,并删除该列表中的一些值.类似的东西

One of my classes accumulates values in a list, uses the list as an argument to a method on another object and deletes some of the values in this list. Something like

element = element_source.get()
self.elements.append(element)
element_destination.send(elements)
self.remove_outdated_elements()

但是当我尝试测试这种行为时,我发现模拟不会复制他们的论点.

But when when i was trying to test this behavior, i've found that mocks don't copy their arguments.

>>> from unittest.mock import Mock
>>> m = Mock()
>>> a = [1]
>>> m(a)
<Mock name='mock()' id='139717658759824'>
>>> m.call_args
call([1])
>>> a.pop()
1
>>> m.assert_called_once_with([1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/unittest/mock.py", line 737, in assert_called_once_with
    return self.assert_called_with(*args, **kwargs)
  File "/usr/lib/python3.3/unittest/mock.py", line 726, in assert_called_with
    raise AssertionError(msg)
AssertionError: Expected call: mock([1])
Actual call: mock([])

有没有办法让 Mock 复制它的调用参数?如果不是,那么测试这种行为的最佳方法是什么?

Is there a way to make Mock copy it's call arguments? If not, what is the best way to test this kind of behavior?

推荐答案

有个章节Coping with mutable arguments" 在文档中,它为您的问题提出了几种解决方案.

There is a chapter "Coping with mutable arguments" in the documentation, which suggests several solutions to your problem.

我会选择这个:

>>> from copy import deepcopy
>>> class CopyingMock(MagicMock):
...     def __call__(self, *args, **kwargs):
...         args = deepcopy(args)
...         kwargs = deepcopy(kwargs)
...         return super(CopyingMock, self).__call__(*args, **kwargs)
...
>>> c = CopyingMock(return_value=None)
>>> arg = set()
>>> c(arg)
>>> arg.add(1)
>>> c.assert_called_with(set())
>>> c.assert_called_with(arg)
Traceback (most recent call last):
    ...
AssertionError: Expected call: mock(set([1]))
Actual call: mock(set([]))
>>> c.foo
<CopyingMock name='mock.foo' id='...'>

这篇关于我如何检查调用参数是否会随 unittest.mock 改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)