久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我如何檢查調用參數是否會隨 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()

但是當我嘗試測試這種行為時,我發(fā)現模擬不會復制他們的論點.

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?

推薦答案

有個章節(jié)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 改變的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 99精品久久久国产一区二区三 | 久久久久久看片 | 黄色在线免费播放 | 激情 一区 | 日韩欧美国产精品 | 亚洲欧美在线一区 | 久久精品亚洲欧美日韩精品中文字幕 | 免费成人午夜 | 综合自拍| 亚洲精品一区二区在线观看 | 国产成人免费视频网站高清观看视频 | 成人午夜性成交 | 成人一区二区三区在线观看 | 国产精品视频网 | 午夜视频在线观看一区二区 | 九九热视频这里只有精品 | 中文字幕视频在线 | 欧美黄色一区 | 日韩视频精品在线 | 日韩一级黄色毛片 | 一区二区视频在线 | 欧美一级在线免费观看 | 中文字幕免费中文 | 亚洲一区电影 | 国产成人福利 | 精品国产一区二区三区成人影院 | 亚洲码欧美码一区二区三区 | 国产精品久久久久一区二区三区 | 凹凸日日摸日日碰夜夜 | 国产成人精品综合 | 亚洲午夜视频 | 2018天天干天天操 | 在线免费国产视频 | 免费看一区二区三区 | 蜜桃精品视频在线 | 国产一级片一区二区三区 | 成人在线国产 | 国产人成精品一区二区三 | 亚洲精品久久久一区二区三区 | 欧洲一区二区视频 | 精品久久久一区 |