5.4 Mocking#

Estimated time for this notebook: 15 minutes

Definition#

Mock: verb,

  1. to tease or laugh at in a scornful or contemptuous manner

  2. to make a replica or imitation of something

Mocking

  • Replace a real object with a pretend object, which records how it is called, and can assert if it is called wrong

Mocking frameworks#

Recording calls with mock#

Mock objects record the calls made to them:

from unittest.mock import Mock

function = Mock(name="myroutine", return_value=2)
function(1)
2
function(5, "hello", a=True)
2
function.mock_calls
[call(1), call(5, 'hello', a=True)]

The arguments of each call can be recovered

name, args, kwargs = function.mock_calls[1]
args, kwargs
((5, 'hello'), {'a': True})

Mock objects can return different values for each call

function = Mock(name="myroutine", side_effect=[2, "xyz"])
function(1)
2
function(1, "hello", {"a": True})
'xyz'

We expect an error if there are no return values left in the list:

function()
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
Cell In[9], line 1
----> 1 function()

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/mock.py:1081, in CallableMixin.__call__(self, *args, **kwargs)
   1079 self._mock_check_sig(*args, **kwargs)
   1080 self._increment_mock_call(*args, **kwargs)
-> 1081 return self._mock_call(*args, **kwargs)

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/mock.py:1085, in CallableMixin._mock_call(self, *args, **kwargs)
   1084 def _mock_call(self, /, *args, **kwargs):
-> 1085     return self._execute_mock_call(*args, **kwargs)

File /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/unittest/mock.py:1142, in CallableMixin._execute_mock_call(self, *args, **kwargs)
   1140     raise effect
   1141 elif not _callable(effect):
-> 1142     result = next(effect)
   1143     if _is_exception(result):
   1144         raise result

StopIteration: 

Using mocks to model test resources#

Often we want to write tests for code which interacts with remote resources. (E.g. databases, the internet, or data files.)

We don’t want to have our tests actually interact with the remote resource, as this would mean our tests failed due to lost internet connections, for example.

Instead, we can use mocks to assert that our code does the right thing in terms of the messages it sends: the parameters of the function calls it makes to the remote resource.

For example, consider the following code that downloads a map from the internet:

import requests


def map_at(lat, long, satellite=False, zoom=12, size=(400, 400)):
    base = "https://static-maps.yandex.ru/1.x/?"
    params = dict(
        z=zoom,
        size=str(size[0]) + "," + str(size[1]),
        ll=str(long) + "," + str(lat),
        l="sat" if satellite else "map",
        lang="en_US",
    )
    return requests.get(base, params=params, timeout=60)
london_map = map_at(51.5073509, -0.1277583)
%matplotlib inline
import IPython

IPython.core.display.Image(london_map.content)
../_images/05_04_mocking_25_0.png

We would like to test that it is building the parameters correctly. We can do this by mocking the requests object. We need to temporarily replace a method in the library with a mock. We can use “patch” to do this:

from unittest.mock import patch

with patch.object(requests, "get") as mock_get:
    london_map = map_at(51.5073509, -0.1277583)
    print(mock_get.mock_calls)
[call('https://static-maps.yandex.ru/1.x/?', params={'z': 12, 'size': '400,400', 'll': '-0.1277583,51.5073509', 'l': 'map', 'lang': 'en_US'}, timeout=60)]

Our tests then look like:

def test_build_default_params():
    with patch.object(requests, "get") as mock_get:
        map_at(51.0, 0.0)
        mock_get.assert_called_with(
            "https://static-maps.yandex.ru/1.x/?",
            params={
                "z": 12,
                "size": "400,400",
                "ll": "0.0,51.0",
                "l": "map",
                "lang": "en_US",
            },
            timeout=60,
        )


test_build_default_params()

That was quiet, so it passed. When I’m writing tests, I usually modify one of the expectations, to something ‘wrong’, just to check it’s not passing “by accident”, run the tests, then change it back!

Testing functions that call other functions#

def partial_derivative(function, at, direction, delta=1.0):
    f_x = function(at)
    x_plus_delta = at[:]
    x_plus_delta[direction] += delta
    f_x_plus_delta = function(x_plus_delta)
    return (f_x_plus_delta - f_x) / delta

We want to test that the above function does the right thing. It is supposed to compute the derivative of a function of a vector in a particular direction.

E.g.:

partial_derivative(sum, [0, 0, 0], 1)
1.0

How do we assert that it is doing the right thing? With tests like this:

from unittest.mock import MagicMock


def test_derivative_2d_y_direction():
    func = MagicMock()
    partial_derivative(func, [0, 0], 1)
    func.assert_any_call([0, 1.0])
    func.assert_any_call([0, 0])


test_derivative_2d_y_direction()

We made our mock a “Magic Mock” because otherwise, the mock results f_x_plus_delta and f_x can’t be subtracted:

MagicMock() - MagicMock()
<MagicMock name='mock.__sub__()' id='139886070574528'>
Mock() - Mock()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[19], line 1
----> 1 Mock() - Mock()

TypeError: unsupported operand type(s) for -: 'Mock' and 'Mock'