Python - 如何断言未使用特定参数调用模拟对象?

Python - How can I assert a mock object was not called with specific arguments?

我意识到 unittest.mock 对象现在有一个可用的 assert_not_called 方法,但我正在寻找的是 assert_not_called_with。有没有这样的东西?我查看了 Google 并没有看到任何东西,当我尝试仅使用 mock_function.assert_not_called_with(...) 时它引发了一个 AttributeError,这意味着该名称不存在该函数。

我目前的解决方案

with self.assertRaises(AssertionError):
    mock_function.assert_called_with(arguments_I_want_to_test)

这行得通,但如果我想进行多个此类调用,就会使代码混乱。

相关

Assert a function/method was not called using Mock

你可以自己给unittest.mock.Mock添加一个assert_not_called_with方法:

from unittest.mock import Mock

def assert_not_called_with(self, *args, **kwargs):
    try:
        self.assert_called_with(*args, **kwargs)
    except AssertionError:
        return
    raise AssertionError('Expected %s to not have been called.' % self._format_mock_call_signature(args, kwargs))

Mock.assert_not_called_with = assert_not_called_with

这样:

m = Mock()
m.assert_not_called_with(1, 2, a=3)
m(3, 4, b=5)
m.assert_not_called_with(3, 4, b=5)

输出:

AssertionError: Expected mock(3, 4, b=5) to not have been called.

使用 Pytest,我断言调用了“AssertionError”:

import pytest
from unittest.mock import Mock


def test_something():
    something.foo = Mock()
    
    # Test that something.foo(bar) is not called.
    with pytest.raises(AssertionError):
        something.foo.assert_called_with(bar)

另一种使用模拟调用历史记录的解决方案:

from unittest.mock import call

assert call(arguments_I_want_to_test) not in mock_function.mock_calls