Testing with test doubles

A test double (mock, stub) replaces a real dependency, such as a network call, during a test. This makes the test fast, deterministic and independent of the external service. In Python, unittest.mock.patch or pytest-mock substitute the called function with an object that returns a controlled response.

Example

with patch('mon_module.requests.get') as get:
    get.return_value.json.return_value = {'ok': True}

Related concepts

The whole glossary