mocking
Mocking is a testing technique that replaces real objects with stand-ins called mocks, which let you control behavior, record calls, and isolate the code under test from its dependencies.
Mocks are useful when the real dependency is slow, nondeterministic, expensive, or not available yet. A network call, the current time, and a paid API are common examples. Swapping the dependency for a mock lets tests run quickly and check that your code calls the dependency the way you expect.
Example
Python’s standard library provides the unittest.mock module for mocking. Suppose you’re testing a get_display_name() function that looks up a user through an external API. A real test shouldn’t make a network call, so it passes a Mock in place of the API client, configures the data the client returns, and then checks how the function used it:
test_users.py
import unittest
from unittest.mock import Mock
def get_display_name(api, user_id):
user = api.get_user(user_id)
return user["name"].title()
class TestGetDisplayName(unittest.TestCase):
def test_formats_name_from_api(self):
api = Mock() # API mock
api.get_user.return_value = {"name": "john"} # Canned response
result = get_display_name(api, 123)
self.assertEqual(result, "John")
api.get_user.assert_called_once_with(123)
if __name__ == "__main__":
unittest.main()
Running the test confirms it passes without any network access:
$ python test_users.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
The Mock stands in for the API client, and api.get_user.return_value controls the data it hands back, so get_display_name() runs in isolation. After the call, .assert_called_once_with() verifies that the function requested the right user ID. The dot in the runner output marks the one passing test.
Related Resources
Tutorial
Understanding the Python Mock Object Library
In this tutorial, you'll learn how to use the Python mock object library, unittest.mock, to create and use mock objects to improve your tests. Obstacles like complex logic and unpredictable dependencies make writing valuable tests difficult, but unittest.mock can help you overcome these obstacles.
For additional information on related topics, take a look at the following resources:
- Python's unittest: Writing Unit Tests for Your Code (Tutorial)
- pytest Tutorial: Effective Python Testing (Tutorial)
- Write Unit Tests for Your Python Code With ChatGPT (Tutorial)
- How to Provide Test Fixtures for Django Models in Pytest (Tutorial)
- Getting Started With Testing in Python (Tutorial)
- Improving Your Tests With the Python Mock Object Library (Course)
- Exploring unittest.mock in Python (Course)
- Understanding the Python Mock Object Library (Quiz)
- Testing Your Code With Python's unittest (Course)
- Python's unittest: Writing Unit Tests for Your Code (Quiz)
- Testing Your Code With pytest (Course)
- Effective Testing with Pytest (Quiz)
- Test-Driven Development With pytest (Course)
- Getting Started With Testing in Python (Quiz)