tessafyi
tessafyi

Reputation: 2343

What happens when a python mock has both a return value and a list of side effects?

I'm having trouble understanding what's happening in some test code. It looks like this:

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)

I know that:

But here's what I don't get:

Upvotes: 27

Views: 24695

Answers (1)

chepner
chepner

Reputation: 531045

side_effect is used. A list value can contain mock.DEFAULT, and a function can return mock.DEFAULT, to indicate that the value of the return_value attribute be used.

>>> 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'

Upvotes: 38

Related Questions