当 python 模拟同时具有返回值和副作用列表时会发生什么?

ale*_*ian 8 python unit-testing mocking pytest python-unittest.mock

我无法理解某些测试代码中发生的情况。它看起来像这样:

import pytest
from unittest.mock import MagicMock
from my_module import MyClass

confusing_mock = MagicMock(
    return_value=b"",
    side_effect=[
        ConnectionError(),
        b"another_return_value?",
        b"another_another_return_value?"
    ])

mocked_class = MyClass()
monkeypatch.setattr(mocked_class, "method_to_call_thrice", confusing_mock)
Run Code Online (Sandbox Code Playgroud)

我知道:

  • side_effect 是在调用模拟时要调用的函数
  • 但是如果side_effect是可迭代的,那么“对模拟的每次调用都将返回可迭代的下一个值”(感谢pytest docs
  • 文档还说,如果传递给的函数side_effect 返回DEFAULT,则模拟将从 return_value

但这是我没有得到的:

  • 当我为发生什么一个副作用名单返回值
  • 我应该在每次调用 时看到MyClass.method_to_call_thrice什么?

che*_*ner 16

side_effect用来。列表值可以包含mock.DEFAULT,函数可以返回mock.DEFAULT,以指示return_value要使用的属性值。

>>> import unittest.mock
>>> m = unittest.mock.Mock(return_value="foo",
...                        side_effect=[1, 2, unittest.mock.DEFAULT, 4, 5])
>>> m()
1
>>> m()
2
>>> m()
'foo'
>>> m()
4
>>> m()
5
>>> unittest.mock.Mock(return_value="foo",
...                    side_effect=lambda: unittest.mock.DEFAULT)()
'foo'
Run Code Online (Sandbox Code Playgroud)

  • 要删除已经存在的“side_effect”,您还可以将其设置为“None”(根据文档[此处](https://docs.python.org/3/library/unittest.mock.html#unittest.mock。嘲笑)) (2认同)