gpt4 book ai didi

python - 模拟 : assert mock_calls with a numpy array as argument raises ValueError and np. testing.assert_array_equal 不起作用

转载 作者:行者123 更新时间:2023-12-01 07:42:08 24 4
gpt4 key购买 nike

我有一个模拟对象,我想使用mock_calls检查它的调用,它是用numpy数组调用的。但问题是它会引发 ValueError,如下面的简单玩具示例所示。

>>> mocked_model_called_with_np_array = mock.Mock()
>>> mocked_model_called_with_np_array(np.array([1, 2]))
>>> mocked_model_called_with_np_array.mock_calls
[call(array([1, 2]))]

现在我设置了预期的调用:

>>> expected_call_with_numpy = [mock.call(np.array([1, 2]))]

现在,如果我如下所示检查它,则会引发错误:

>>> assert expected_call_with_numpy == mocked_model_called_with_np_array.mock_calls

---------------------------------------------------------------------------
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-61-9806e62badf5> in <module>
----> 1 assert expected_call_with_numpy == mocked_model_called_with_np_array.mock_calls

c:\..\python\python36\lib\unittest\mock.py in __eq__(self, other)
2053
2054 # this order is important for ANY to work!
-> 2055 return (other_args, other_kwargs) == (self_args, self_kwargs)
2056
2057

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我在 stackoverflow 上的搜索和找到的解决方案:

HERE当你有 numpy 数组时,建议使用 np.testing.assert_array_equal ,但这也不能解决我的问题,如下所示。

>>> np.testing.assert_array_equal(expected_call_with_numpy, mocked_model_called_with_np_array.mock_calls)

---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-57-4a0373c94354> in <module>
----> 1 np.testing.assert_array_equal(expected_call_with_numpy, mocked_model_called_with_np_array.mock_calls)

c:\...\python\python36\lib\site-packages\numpy\testing\utils.py in assert_array_equal(x, y, err_msg, verbose)
852 __tracebackhide__ = True # Hide traceback for py.test
853 assert_array_compare(operator.__eq__, x, y, err_msg=err_msg,
--> 854 verbose=verbose, header='Arrays are not equal')
855
856

c:\...\python\python36\lib\site-packages\numpy\testing\utils.py in assert_array_compare(comparison, x, y, err_msg, verbose, header, precision, equal_nan, equal_inf)
776 names=('x', 'y'), precision=precision)
777 if not cond:
--> 778 raise AssertionError(msg)
779 except ValueError:
780 import traceback

AssertionError:
Arrays are not equal

(mismatch 100.0%)
x: array([['', (array([1, 2]),), {}]], dtype=object)
y: array([['', (array([1, 2]),), {}]], dtype=object)

请注意,数组是相同的,但会产生错误!

任何人都可以评论如何将mock_calls用于使用numpy数组调用的mockec对象,然后检查mock_calls是否产生预期的调用?例如,如下所示

assert expected_call_with_numpy == mocked_model_called_with_np_array.mock_calls

最佳答案

在尝试检查模拟调用是否包含特定的 numpy 数组时遇到了同样的问题。事实证明,调用对象是可索引的,因此您也可以执行以下操作:

>>> import numpy as np
>>> from unittest import mock
>>> mock_object = mock.MagicMock()
>>> mock_object(1, np.array([1, 2, 3]), a=3)
>>> mock_object(10, np.array([10, 20, 30]), b=30)
>>> calls = mock_object.call_args_list
>>> calls
[call(1, array([1, 2, 3]), a=3), call(10, array([10, 20, 30]), b=30)]
>>> calls[0]
call(1, array([1, 2, 3]), a=3)
>>> calls[0][0]
(1, array([1, 2, 3]))
>>> calls[0][1]
{'a': 3}

因此,您不必断言调用相等,您只需检查调用是否已进行以及传递的参数是否单独正确,丑陋但有效:

>>> assert mock_object.call_count == 2
>>> assert calls[0][0][0] == 1
>>> np.testing.assert_array_equal(calls[0][0][1], np.array([1, 2, 3]))
>>> assert calls[0][1]['a'] == 3
...

关于python - 模拟 : assert mock_calls with a numpy array as argument raises ValueError and np. testing.assert_array_equal 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56644729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com