What is the reason for plain Mock existing?

Mock's author, Michael Foord, addressed a very similar question at Pycon 2011 (31:00):

Q: Why was MagicMock made a separate thing rather than just folding the ability into the default mock object?

A: One reasonable answer is that the way MagicMock works is that it preconfigures all these protocol methods by creating new Mocks and setting them, so if every new mock created a bunch of new mocks and set those as protocol methods and then all of those protocol methods created a bunch more mocks and set them on their protocol methods, you've got infinite recursion...

What if you want accessing your mock as a container object to be an error -- you don't want that to work? If every mock has automatically got every protocol method, then it becomes much more difficult to do that. And also, MagicMock does some of this preconfiguring for you, setting return values that might not be appropriate, so I thought it would be better to have this convenience one that has everything preconfigured and available for you, but you can also take a ordinary mock object and just configure the magic methods you want to exist...

The simple answer is: just use MagicMock everywhere if that's the behavior you want.

Answer from Ryne Everett on Stack Overflow
Top answer
1 of 5
163

What is the reason for plain Mock existing?

Mock's author, Michael Foord, addressed a very similar question at Pycon 2011 (31:00):

Q: Why was MagicMock made a separate thing rather than just folding the ability into the default mock object?

A: One reasonable answer is that the way MagicMock works is that it preconfigures all these protocol methods by creating new Mocks and setting them, so if every new mock created a bunch of new mocks and set those as protocol methods and then all of those protocol methods created a bunch more mocks and set them on their protocol methods, you've got infinite recursion...

What if you want accessing your mock as a container object to be an error -- you don't want that to work? If every mock has automatically got every protocol method, then it becomes much more difficult to do that. And also, MagicMock does some of this preconfiguring for you, setting return values that might not be appropriate, so I thought it would be better to have this convenience one that has everything preconfigured and available for you, but you can also take a ordinary mock object and just configure the magic methods you want to exist...

The simple answer is: just use MagicMock everywhere if that's the behavior you want.

2 of 5
109

With Mock you can mock magic methods but you have to define them. MagicMock has "default implementations of most of the magic methods.".

If you don't need to test any magic methods, Mock is adequate and doesn't bring a lot of extraneous things into your tests. If you need to test a lot of magic methods MagicMock will save you some time.

🌐
Medium
medium.com › @snehagiranje05 › unveiling-the-magic-understanding-mock-and-magicmock-in-python-ecadf1f1013c
Unveiling the Magic: Understanding Mock and MagicMock in Python | by Snehagiranje | Medium
June 18, 2024 - MagicMock is a subclass of Mock that includes default implementations of most of the "magic methods" (or "dunder methods"), which are the special methods with double underscores (e.g., __len__, __getitem__, etc.).
Discussions

mocking - What is difference between python mock and magic mock? - Stack Overflow
What is difference between python mock and magic mock, because I could see ... Save this answer. ... Show activity on this post. MagicMock is a subclass of Mock with default implementations of many magic methods. More on stackoverflow.com
🌐 stackoverflow.com
pytest mock
pytest-mock is a wrapper around unittest.mock, there's only small differences in the interface. IMO, it's important not to dwell on tiny differences like what framework to use for testing or mocking because the bigger questions are: What should you test and how should you structure it? What should you mock and how should you mock it? How can you structure your code to be more testable and need less mocking? (which tends to result in a greater degree of configurability, to boot) All of that said, pytest and its many plugins (including pytest-mock) are my go to. More on reddit.com
🌐 r/Python
28
58
December 28, 2023
I made a type-safe mocking library
I think megamock does the same thing? https://old.reddit.com/r/Python/comments/1245ugm/seeking_early_feedback_on_megamock_a_dev/ More on reddit.com
🌐 r/Python
5
6
May 15, 2023
Monkeypatch vs Mock?

Monkey patching is a general concept of overwriting an implementation of X with something else at runtime. A very dumb example is something like: Monkey patching the sleep function to not do anything to prevent any part of your code from ever sleeping.

Mocking is a form of monkey patching more specifically meant for tests, where you overwrite real implementations that do stuff with an implementation that allows you to easily check whether the object/function/class whatever is acted upon as expected. e.g. if an object has a do_stuff() function, an example mock would be to create a mock object that doesn't do anything in do_stuff(), but it'll have a boolean flag that says "do_stuff() was called". Then, your test would check if do_stuff() was called.

More on reddit.com
🌐 r/learnpython
7
3
June 24, 2020
People also ask

What is the actual difference between Mock and MagicMock?
MagicMock is a subclass of Mock that pre-configures the common dunder methods (__enter__, __exit__, __iter__, __len__, __getitem__, arithmetic, and more). Plain Mock does not support dunder methods, so it cannot be used directly in a with statement, for loop, or len() call without manual wiring.
🌐
python-testing-debugging.com
python-testing-debugging.com › home › advanced mocking test doubles in python › deep dive into unittestmock › when to use magicmock vs mock in python
When to Use MagicMock vs Mock in Python | PTD
Does MagicMock auto-create attributes the way Mock does?
Yes. Both classes auto-create child mocks for any undefined non-dunder attribute. The difference is only the pre-wired dunder protocol methods. To stop auto-creation on either class you must pass spec= or spec_set=.
🌐
python-testing-debugging.com
python-testing-debugging.com › home › advanced mocking test doubles in python › deep dive into unittestmock › when to use magicmock vs mock in python
When to Use MagicMock vs Mock in Python | PTD
When should I prefer plain Mock over MagicMock?
Prefer Mock when the dependency uses no Python protocols and you want a leaner object, or when you want a dunder access like __iter__ to fail loudly instead of silently returning a configured iterator. Add spec= for contract enforcement regardless of which class you pick.
🌐
python-testing-debugging.com
python-testing-debugging.com › home › advanced mocking test doubles in python › deep dive into unittestmock › when to use magicmock vs mock in python
When to Use MagicMock vs Mock in Python | PTD
🌐
Medium
medium.com › @silverlong326 › mock-vs-magicmock-the-tale-of-two-testing-tools-and-why-youve-been-using-them-wrong-afd72ee461c1
Mock vs MagicMock: The Tale of Two Testing Tools (and Why You’ve Been Using Them Wrong) | by Long Ren | Medium
December 24, 2024 - TL;DR: Mock is your blank canvas — use it when you need precise control and minimal magic. MagicMock comes pre-loaded with magic methods — perfect for quick setups and real-world Python object simulation.
🌐
Python
docs.python.org › 3 › library › unittest.mock.html
unittest.mock — mock object library
Mock allows you to assign functions (or other Mock instances) to magic methods and they will be called appropriately. The MagicMock class is just a Mock variant that has all of the magic methods pre-created for you (well, all the useful ones anyway).
🌐
Real Python
realpython.com › python-mock-library
Understanding the Python Mock Object Library – Real Python
May 29, 2026 - The main difference between Mock and MagicMock is that MagicMock is a subclass of Mock that has default implementations for most magic methods, making it more suitable for use cases involving special methods.
🌐
Anna-Lena Popkes
alpopkes.com › posts › python › mocking
Mocking in Python
October 11, 2020 - The third main functionality contained in the unittest.mock library is the patch function. So what is patching? The patch() function looks up an object in a given module and replaces it with another object. By default, the object is replaced with a new MagicMock (but this can be changed).
Find elsewhere
🌐
Medium
mike-diaz006.medium.com › what-i-learned-at-work-this-week-magicmock-61996506bc27
What I Learned at Work this Week: MagicMock | by Mike Diaz | Medium
April 8, 2023 - In my last post, I wrote about Mock, which we can use to replace objects when writing tests in Python. MagicMock is a sublcass of Mock, so it does all the same things, but includes some default results for common methods:
🌐
Python-testing-debugging
python-testing-debugging.com › home › advanced mocking test doubles in python › deep dive into unittestmock › when to use magicmock vs mock in python
When to Use MagicMock vs Mock in Python | PTD
June 18, 2026 - Both classes auto-create child ... the rule is: use MagicMock when your code drives the double through a protocol (with, for, len, [], +); use Mock when the double is only called or has plain attributes read....
🌐
DEV Community
dev.to › lizzzzz › mock-it-till-you-make-it-mocking-in-pythons-unittest-42d4
Mock It Till You Make It: Mocking in Python's unittest - DEV Community
April 29, 2024 - MagicMock is a subclass of Mock “with default implementations of most of the magic methods.” (Read the documentation here.) “Magic methods” are the ones that come baked in – methods like mathematical operations, iteration, and dictionary ...
🌐
Safeguard
safeguard.sh › blog › devsecops › mocking in python: unittest.mock, magicmock, and return values
Python Unit Test Mock: unittest.mock and MagicMock Guide
June 14, 2026 - Practical rule — reach for MagicMock when the thing you are replacing gets used as a context manager, an iterable, or with len()/indexing; use Mock when you want an error if the code touches a dunder you did not expect, which occasionally ...
🌐
Python
docs.python.org › 3 › library › unittest.mock-examples.html
unittest.mock — getting started
In most of these examples the Mock and MagicMock classes are interchangeable.
🌐
sqlpey
sqlpey.com › python › solved-the-ultimate-guide-to-using-mock-and-magicmock-in-python
Solved: The Ultimate Guide to Using Mock and MagicMock in Python
December 5, 2024 - To clarify, MagicMock is not merely a stripped-down version of Mock; instead, it boasts additional functionality aimed at handling magic methods. What distinguishes MagicMock is its ability to automatically implement significant magic methods, which means it can handle operations that involve ...
🌐
Kbstechnocraft
kbstechnocraft.com › 2023 › 04 › mock-patch-and-magicmock-in-python.html
KBS Technocraft: Mock, patch, and MagicMock in python
April 14, 2023 - Additionally, because patching is built into the mock library, you don't need to learn a new testing framework or tool to start using it. MagicMock is a subclass of Mock that allows you to automatically create mock objects with all the methods and attributes of the objects they are replacing.
🌐
Opensource.com
opensource.com › article › 23 › 4 › using-mocks-python
Using mocks in Python | Opensource.com
April 1, 2023 - Sometimes, having a MagicMock that returns the same thing every time isn't quite everything you need it to be. For example, sys.stdin.readline() usually returns different values, not the same value throughout the test. The property side_effect allows controlling what a magic mock returns on a more detailed level than using return_value.
🌐
Garybernhardt
garybernhardt.github.io › python-mock-comparison
Python Mock Library Comparison v1.0 documentation
>>> # Mock >>> my_mock = mock.MagicMock() >>> with my_mock: ... pass >>> # Flexmock >>> my_mock = flexmock() >>> with my_mock: ... pass >>> # Mox >>> my_mock = mox.MockAnything() >>> with my_mock: ... pass >>> # Dingus >>> my_dingus = dingus.Dingus() >>> with my_dingus: ... pass # >>> # Fudge # >>> # XXX Currently failing in Python 2.7 # >>> my_fake = fudge.Fake().provides('__enter__').provides('__exit__') # >>> with my_fake: # ...
🌐
Bright Yellow
ines-panker.com › 2020 › 06 › 01 › python-mock.html
Cheat Sheet of Python Mock by Ines Panker
June 1, 2020 - Use MagicMock when your code does len(obj) or obj[0]. Otherwise, Mock is fine.
🌐
Medium
datageeks.medium.com › python-unittest-a-guide-to-patching-mocking-and-magicmocks-40f2c0738981
Python Unittest: A Guide to Patching, Mocking, and MagicMocks | by DataGeeks | Medium
December 12, 2023 - Setting Up the Environment: Before diving into unit testing, ensure you have a suitable environment set up. You should have Python installed, and you may want to create a virtual environment to manage dependencies. You’ll also need the unittest.mock module, which is included in Python 3.3 and later.
🌐
OneUptime
oneuptime.com › home › blog › how to use pytest with mocking
How to Use pytest with Mocking
February 2, 2026 - # Comparing Mock and MagicMock for magic method support from unittest.mock import Mock, MagicMock # Regular Mock does not support magic methods by default regular_mock = Mock() # len(regular_mock) # Would raise TypeError # MagicMock supports magic methods out of the box magic_mock = MagicMock() magic_mock.__len__.return_value = 5 len(magic_mock) # Returns 5 # MagicMock also handles iteration magic_mock.__iter__.return_value = iter([1, 2, 3]) list(magic_mock) # Returns [1, 2, 3] # Context manager support magic_mock.__enter__.return_value = "entered" with magic_mock as value: print(value) # Prints "entered"
🌐
ARCsoft
arcsoft.uvic.ca › log › 2025-03-14-mocking-unit-tests
To Mock or Not to Mock - Mocking Unit Tests • ARCsoft
You have several choices of libraries to use in Python to mock tests. Use mock() if you need more control, and use MagicMock() if you want the convenience of having built-in support for magic methods.