Python mock multiple return values
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A mock often needs to behave differently on successive calls. That happens when you are testing retries, polling loops, pagination, or any code path where the dependency changes state over time. In Python, the usual tool for this is side_effect from unittest.mock.
Use side_effect for Sequential Results
If every call should return the same value, return_value is enough. If the mock should return different values each time, assign an iterable to side_effect.
The first call returns the first item, the second call returns the second item, and so on. This keeps the test focused on behavior instead of on setting up a real external system.
Test Retry Logic Cleanly
One of the most useful cases is retry handling. The dependency can fail once and then succeed on the next call.
This is easier to reason about than using sleeps, real sockets, or a temporary test server just to create one failing request.
Mix Return Values and Exceptions
side_effect can also raise exceptions. That is useful for testing recovery code after a temporary failure.
When a value inside side_effect is an exception instance or exception class, the mock raises it instead of returning it.
Patch the Symbol Used by the Code Under Test
In real tests, you usually patch a function as imported by the module under test, not just construct a loose mock object. That lookup path matters.
If the code under test imported the dependency into a different namespace, patch that namespace instead. Many failed mock tests come from patching the original library symbol while the application code is reading a copied reference.
Use a Function When Output Depends on Input
A list is perfect for strictly sequential behavior, but sometimes the return value should vary by arguments. In that case, give side_effect a function.
This keeps the mock compact while still reacting to its inputs like a lightweight fake.
Common Pitfalls
Using return_value when the test really needs changing results hides important control-flow behavior. Use side_effect when successive calls matter.
If the iterable is too short, the mock raises StopIteration. Make sure the number of configured results matches the expected number of calls.
Patching the wrong import path is a classic Python testing mistake. Patch the name as the code under test resolves it.
Summary
- Use
side_effectwith a list when successive calls should return different values. - Include exceptions in the sequence to test retry and recovery paths.
- Patch the symbol as used by the code under test, not just the original library symbol.
- Use a function as
side_effectwhen return values depend on the call arguments.
Related reading
- Python Mocking a context manager
- Python Mocking a function from an imported module
- Python Mocking out Kafka for integration tests
- Python model.fit error, None values not supported
- python running coverage on never ending process
- Python unittest - opposite of assertRaises?
- Python module for converting PDF to text
- Python multiprocessing PicklingError Can't pickle type 'function
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.