https://docs.python.org/3/library/unittest.mock.html
https://realpython.com/python-mock-library/
Understanding the Python Mock Object Library
unittest.mock is a library for testing in Python (Py3.3+). It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.
Mock is a flexible mock object intended to replace the use of stubs and test doubles throughout your code.
MagicMock is a subclass of Mock with all the magic methods pre-created and ready to use.
Mock can create arbitrary attributes on the fly.
from unittest.mock import Mock mock = Mock() print(mock) print(mock.some_attribute) print(mock.do_something()) # <Mock id='140138960515368'> # <Mock name='mock.some_attribute' id='140138960515592'> # <Mock name='mock.do_something()' id='140138958324288'>
from unittest.mock import MagicMock class ProductionClass: pass thing = ProductionClass() thing.method = MagicMock(return_value=3) thing.method(3, 4, 5, key='value') # returns 3 thing.method.assert_called_with(10, key='x') thing.method(10, key='y') # AssertionError: expected call not found.