简体   繁体   English

如何在python 3.6下获得Mock()。assert_Called_once类似行为?

[英]How to get Mock().assert_called_once like behavior below python 3.6?

How to get unittest.mock.Mock().assert_called_once like behavior with unittest.mock.Mock().assert_called_once_with , as assert_called_once s not available below python 3.6 . 如何使用unittest.mock.Mock().assert_called_once类似行为的unittest.mock.Mock().assert_called_once_with ,因为在python 3.6下不可用assert_called_once

Basically what I want is checking if the method gets called exactly once, no matter with which argument or how many argument. 基本上我想要的是检查该方法是否仅被调用一次,无论使用哪个参数或多少个参数。

I tried unittest.mock.ANY , but seems it isn't what I want. 我尝试了unittest.mock.ANY ,但似乎不是我想要的。

All that assert_called_once() does is assert that the Mock.call_count attribute is 1; assert_called_once()所做的所有assert_called_once()就是断言Mock.call_count属性为1; you can trivially do the same test: 您可以简单地执行相同的测试:

self.assertEqual(
    mock_object.call_count, 1,
    "Expected mock to have been called once. Called {} times.".format(
        mock_object.call_count))

If you tried to use Mock.assert_called_once_with() with the ANY object, take into account that that object stands for the value of one of the arguments . 如果您尝试将Mock.assert_called_once_with()ANY对象一起使用,请考虑到该对象代表其中一个参数的值。 Mock.assert_called_once_with(ANY) only matches if the object has been called with exactly one argument , and the value of that argument doesn't matter: Mock.assert_called_once_with(ANY)仅在对象仅使用一个参数调用且该参数的值无关紧要的情况下才匹配:

>>> from unittest.mock import Mock, ANY
>>> m = Mock()
>>> m(42, 81, spam='eggs')  # 2 positional arguments, one keyword argument
<Mock name='mock()' id='4348527952'>
>>> m.assert_called_once_with(ANY)   # doesn't match, just one positional argument
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.6/unittest/mock.py", line 825, in assert_called_once_with
    return self.assert_called_with(*args, **kwargs)
  File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.6/unittest/mock.py", line 814, in assert_called_with
    raise AssertionError(_error_message()) from cause
AssertionError: Expected call: mock(<ANY>)
Actual call: mock(42, 81, spam='eggs')

You can't use the ANY object together with the *called_with assertions to mean 'any number of arguments'. 您不能将ANY对象与*called_with断言一起使用来表示“任意数量的参数”。 Only in an assertion that takes call() objects would ANY be interpreted as any number of arguments ; 只有在采用call()对象的断言中, ANY才能解释为任意数量的参数 so you could also use Mock.assert_has_calls() here, passing in a list with a single ANY element: 因此,您还可以在此处使用Mock.assert_has_calls() ,并传入带有单个ANY元素的列表:

>>> m.assert_has_calls([ANY])  # no exception raised

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 魔术模拟 assert_Called_once 与 assert_Called_once_with 奇怪的行为 - Magic mock assert_called_once vs assert_called_once_with weird behaviour python mock assert_call_with - python mock assert_called_with Python 3.6初学者:为什么为assert_Called_once_with获得一个空白的AssertionError(单元测试和模拟) - Python 3.6 Beginner: Why getting a blank AssertionError for assert_called_once_with (unit test & mocking) 我如何使用 Python MonkeyPatch + Mock 断言调用了 function - How can I use Python MonkeyPatch + Mock to assert that a function was called Python - 我如何断言模拟 object 没有被特定的 arguments 调用? - Python - How can I assert a mock object was not called with specific arguments? 即使调用了模拟,Python 模拟断言也会失败 - Python mock assert called fails even if mock called Python补丁模拟似乎已被调用,但断言失败 - Python patch mock appears to be called, but assert fails 如何让 mock.assert_called_once() 接受 object 的任何实例? - How can mock.assert_called_once() be told to accept any instance of an object? 为模拟 assert_call_once_with() 指定“类 Foo 的任何实例”? - Specifying "any instance of class Foo" for mock assert_called_once_with()? 为什么我的 mock.assert_call_once() 没有通过? - Why isn't my mock.assert_called_once() passing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM