Testing and Diagnostics Modules

unittest.mock

Creates test doubles and temporarily replaces dependencies. Mock records calls and configurable results; patch restores the replaced binding on scope exit. Patch where the code looks up a dependency, and use specs or autospec to reduce unrealistic mock interfaces.

Formal Syntax & Structure

python syntaxpython
import unittest.mock

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
from unittest.mock import Mockfetch = Mock(return_value="tea")print(fetch(3))fetch.assert_called_once_with(3)
Python Reference