We use mock return values and side effects to control what a fake function gives back during tests. This helps us test parts of code without running everything for real.
When a function calls an external service like a website or database and you want to test without using the real service.
When you want to check how your code behaves if a function returns different results.
When you want to simulate errors or special cases from a function to see how your code handles them.
When the real function is slow or changes data and you want fast, safe tests.
When you want to test only one part of your code and ignore others.
Syntax
PyTest
from unittest.mock import Mock
mock_function = Mock()
mock_function.return_value = value
# or for side effects
mock_function.side_effect = function_or_exception_or_list
return_value sets what the mock gives back when called.
side_effect can be a function to run, an error to raise, or a list of values/errors for each call.
Examples
This mock always returns 10 when called.
PyTest
mock = Mock()
mock.return_value = 10print(mock()) # prints 10
The mock runs the side effect function and returns its result.
B. Exception object should be raised, not assigned to side_effect
C. The second assignment overwrites the first, no error occurs
D. Mock must be called before setting side_effect
Solution
Step 1: Analyze multiple side_effect assignments
The second assignment to side_effect replaces the first one; no error occurs.
Step 2: Understand mock call behavior
After setting side_effect = [1, 2, 3], the mock returns 1 on first call, so print(mock()) outputs 1.
Final Answer:
The second assignment overwrites the first, no error occurs -> Option C
Quick Check:
Last side_effect assignment wins [OK]
Hint: Last side_effect assignment replaces previous ones [OK]
Common Mistakes:
Thinking multiple side_effect assignments cause errors
Expecting exception to be raised immediately
Confusing assignment with raising exceptions
5. You want a mock to return 10 on the first call, raise a ValueError on the second, and return 20 on the third call. Which setup achieves this?
hard
A. mock.side_effect = [10, ValueError('bad'), 20]
B. mock.return_value = 10
mock.side_effect = ValueError('bad')
mock.return_value = 20
C. mock.side_effect = lambda: [10, ValueError('bad'), 20]
D. mock.side_effect = [10, lambda: (_ for _ in ()).throw(ValueError('bad')), 20]
Solution
Step 1: Understand side_effect list behavior
When side_effect is a list, the mock returns successive items from the list on each call. Non-callable items like numbers or exception instances are returned directly.
Step 2: Recognize exception raising in side_effect list
Placing an exception instance directly returns it as a value, not raises it. To raise, use a generator expression that throws the exception.
Step 3: Analyze options
mock.side_effect = [10, ValueError('bad'), 20] returns the exception object instead of raising it. mock.side_effect = [10, lambda: (_ for _ in ()).throw(ValueError('bad')), 20] uses a generator expression to raise ValueError on second call, returning 10 and 20 on first and third calls respectively.
Final Answer:
mock.side_effect = [10, lambda: (_ for _ in ()).throw(ValueError('bad')), 20] -> Option D
Quick Check:
Use generator throw to raise exceptions in side_effect list [OK]
Hint: Use generator throw to raise exceptions in side_effect list [OK]
Common Mistakes:
Putting exception instance directly in side_effect list
Trying to set multiple return_values
Using lambda returning list instead of generator throw
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.