如何验证Python中未调用的模拟方法?

2 投票
1 回答
4389 浏览
提问于 2025-04-18 07:32

在我的代码中,我使用了assert_any_call()来检查对Django模型过滤器的一系列调用。现在我想验证相反的情况,比如assert_not_called(args)。

在Python中有没有什么断言语句可以实现这个功能呢?

1 个回答

3

最简单的方法是使用 Mock.call_args_list

assert call(None, a=1, b="") not in mocked_func.call_args_list, "Called with invalid args."

如果你想要一个方法,可以使用:

class NotCalledMagicMock(unittest.mock.MagicMock):
    def assert_not_called(_mock_self, *args, **kwargs):
        self = _mock_self
        if self.call_args is None:
            return

        expected = self._call_matcher((args, kwargs))
        if any(self._call_matcher(ca) == expected for ca in self.call_args_list):
            cause = expected if isinstance(expected, Exception) else None
            raise AssertionError(
                '%r found in call list' % (self._format_mock_call_signature(args, kwargs),)
            ) from cause

要使用这个类,在你的测试函数前面加上这个装饰器:

@unittest.mock.patch("unittest.mock.MagicMock", NotCalledMagicMock)

或者可以这样创建你的模拟对象:

func_b_mock = NotCalledMagicMock()

要使用这个方法(这里的 func_b_mock 是通过比如 patch 生成的模拟对象):

func_b_mock.assert_not_called([12], a=4)

当它失败时,会抛出一个 AssertionError,像这样:

Traceback (most recent call last):
  File "your_test.py", line 34, in <module>
    test_a()
  File "/usr/lib/python3.4/unittest/mock.py", line 1136, in patched
    return func(*args, **keywargs)
  File "your_test.py", line 33, in test_a
    func_b_mock.assert_not_called([1])
  File "your_test.py", line 20, in assert_not_called
    ) from cause
AssertionError: 'func_b([1])' found in call list

撰写回答