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:
side_effect
is a function to be called whenever the mock is called- but if
side_effect
is an iterable, then "each call to the mock will return the next value from the iterable" (thanks pytest docs) - the docs also say that if the function passed to
side_effect
returnsDEFAULT
, then the mock will return it's normal value fromreturn_value
But here's what I don't get:
- What happens when I provide both a list of side effects and a return value?
- What should I expect to see on each call of
MyClass.method_to_call_thrice
?