Mock vs MagicMock in Python: Key Differences

In Python's unittest.mock library, both Mock and MagicMock are core classes used to replace real objects during testing, but they handle special "dunder" (magic) methods differently. While MagicMock is a subclass of Mock that comes pre-configured with default implementations for most magic methods (such as __len__, __iter__, and __enter__), a standard Mock does not include these methods by default. This guide covers the technical differences between the two classes, practical examples of how they behave, and when to use each in your test suite.

The Core Difference: Magic Methods

Python relies heavily on special double-underscore methods to implement protocols such as iteration, context management, and operator overloading.

Behavior Comparison in Code

1. Context Managers (__enter__ and __exit__)

from unittest.mock import Mock, MagicMock

# Using MagicMock
with MagicMock() as magic:
    print("MagicMock works in context managers")

# Using Mock
try:
    with Mock() as plain_mock:
        pass
except TypeError as e:
    print(f"Mock fails: {e}")
    # Output: Mock fails: 'Mock' object does not support the context manager protocol

2. Sequence Operations (__len__ and __getitem__)

# MagicMock supports len() out of the box
magic = MagicMock()
print(len(magic))  # Output: 0

# Plain Mock requires manual definition
plain = Mock()
try:
    len(plain)
except TypeError as e:
    print(f"Mock fails: {e}")
    # Output: Mock fails: object of type 'Mock' has no len()

# To make Mock work with len(), you must set the method explicitly:
plain.__len__ = Mock(return_value=5)
print(len(plain))  # Output: 5

3. Iteration (__iter__)

magic = MagicMock()
# MagicMock is iterable by default (returns an empty iterator)
for item in magic:
    pass

plain = Mock()
# Mock raises a TypeError because __iter__ is not defined
try:
    for item in plain:
        pass
except TypeError as e:
    print(f"Mock fails: {e}")
    # Output: Mock fails: 'Mock' object is not iterable

When to Use MagicMock

MagicMock is the default choice for most unit testing scenarios in Python. Use it when:

When to Use Mock

Use standard Mock when you need fine-grained control or when default magic behavior can mask bugs. Use it when: