A mock return value is a preset value that a mocked function returns when called during a test. It helps simulate behavior without running the real code.
Click to reveal answer
intermediate
What does the side_effect attribute do in a pytest mock?
The side_effect attribute lets you define custom behavior for a mock, like raising exceptions or returning different values on each call.
Click to reveal answer
intermediate
How do you set a mock to return different values on consecutive calls?
Use side_effect with a list of values. Each call to the mock returns the next value in the list.
Click to reveal answer
beginner
Example: What will this mock return on the first and second call?
mocked_function.side_effect = [10, 20]
On the first call, it returns 10. On the second call, it returns 20. After that, it raises StopIteration.
Click to reveal answer
intermediate
Why use side_effect to raise an exception in a mock?
To test how your code handles errors without causing real failures. It simulates exceptions from dependencies.
Click to reveal answer
In pytest, how do you set a mock to always return the value 5?
Amock.return_value = 5
Bmock.side_effect = 5
Cmock.return = 5
Dmock.value = 5
✗ Incorrect
Use return_value to set a fixed return value for the mock.
What happens if side_effect is set to a list of values?
AThe mock returns values from the list on each call, then raises StopIteration.
BThe mock always returns the whole list.
CThe mock raises an exception immediately.
DThe mock ignores the list and returns None.
✗ Incorrect
side_effect as a list makes the mock return each value in order, then raises StopIteration when exhausted.
How can you simulate an exception being raised by a mock?
ASet side_effect to the exception class or instance.
BSet return_value to the exception.
CCall mock.raise_exception()
DSet mock.error = True
✗ Incorrect
Setting side_effect to an exception causes the mock to raise it when called.
Which attribute would you use to make a mock return different values on multiple calls?
Aside_effect
Breturn_value
Ccall_count
Dmock_values
✗ Incorrect
side_effect can be a list to return different values on each call.
If you want a mock to behave like a function that sometimes raises an error, what should you do?
AUse side_effect with a function that raises the error conditionally.
BSet return_value to None.
CSet side_effect to a list of only values.
DUse return_value with an error message.
✗ Incorrect
side_effect can be a function that raises exceptions or returns values based on logic.
Explain how to use mock return_value and side_effect in pytest to control mock behavior.
Think about how to simulate normal and error cases with mocks.
You got /4 concepts.
Describe a real-life scenario where you would use side_effect to raise an exception in a mock.
Imagine testing how your app reacts when a service is down.
You got /4 concepts.
Practice
(1/5)
1. What does setting mock.return_value do in a pytest mock object?
easy
A. It delays the mock response by a set time.
B. It defines a fixed value that the mock returns when called.
C. It records the number of times the mock was called.
D. It raises an exception when the mock is called.
Solution
Step 1: Understand the role of return_value
The return_value attribute sets a fixed return value for the mock when it is called.
Step 2: Differentiate from other mock features
Unlike side_effect which can raise exceptions or return different values, return_value always returns the same fixed value.
Final Answer:
It defines a fixed value that the mock returns when called. -> Option B
Quick Check:
return_value = fixed return [OK]
Hint: return_value sets fixed output for mock calls [OK]
Common Mistakes:
Confusing return_value with side_effect
Thinking return_value raises exceptions
Assuming return_value tracks call count
2. Which of the following is the correct way to set a mock to raise an exception when called?
easy
A. mock.side_effect = Exception('Error')
B. mock.return_value = Exception('Error')
C. mock.raise_exception = Exception('Error')
D. mock.throw = Exception('Error')
Solution
Step 1: Identify how to raise exceptions with mocks
Setting side_effect to an exception causes the mock to raise that exception when called.
Step 2: Check syntax correctness
Only mock.side_effect = Exception('Error') is valid syntax to raise an exception on call.
Final Answer:
mock.side_effect = Exception('Error') -> Option A
Quick Check:
side_effect raises exceptions [OK]
Hint: Use side_effect to raise exceptions in mocks [OK]
Common Mistakes:
Using return_value to raise exceptions
Using non-existent attributes like raise_exception
Confusing syntax for exception raising
3. What will be the output of this pytest mock code?
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.