Challenge - 5 Problems
Mock Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this pytest mock with return_value?
Consider the following pytest test code using mock. What will be printed when the test runs?
PyTest
from unittest.mock import Mock def fetch_data(): return 'real data' mock_fetch = Mock(return_value='mocked data') result = mock_fetch() print(result)
Attempts:
2 left
💡 Hint
The mock object returns what you set as return_value when called.
✗ Incorrect
The mock object mock_fetch is set to return 'mocked data' whenever called. So calling mock_fetch() prints 'mocked data'.
❓ assertion
intermediate2:00remaining
Which assertion correctly tests a mock's side effect raising an exception?
You have a mock object with side_effect set to raise a ValueError. Which assertion correctly verifies that calling the mock raises this error?
PyTest
from unittest.mock import Mock import pytest mock_func = Mock(side_effect=ValueError('error'))
Attempts:
2 left
💡 Hint
Use pytest.raises context manager to check exceptions.
✗ Incorrect
The pytest.raises context manager is used to assert that a block of code raises a specific exception. Calling mock_func() inside it will raise ValueError as set by side_effect.
🔧 Debug
advanced2:00remaining
Why does this mock side_effect list cause an error on the third call?
Examine the code below. What error occurs on the third call to mock_func and why?
PyTest
from unittest.mock import Mock mock_func = Mock(side_effect=[1, 2]) print(mock_func()) print(mock_func()) print(mock_func())
Attempts:
2 left
💡 Hint
side_effect list acts like an iterator and raises StopIteration when empty.
✗ Incorrect
When side_effect is a list, mock uses it as an iterator. After the last item, calling the mock raises StopIteration.
🧠 Conceptual
advanced2:00remaining
What is the difference between return_value and side_effect in mocks?
Choose the best explanation of how return_value and side_effect differ in mock objects.
Attempts:
2 left
💡 Hint
Think about how you can simulate different behaviors with side_effect.
✗ Incorrect
return_value sets a fixed return value. side_effect can be a function to run, an exception to raise, or an iterable to return different values on each call.
❓ framework
expert3:00remaining
How to mock a function to return different values on consecutive calls in pytest?
You want to mock a function so that it returns 10 on the first call, 20 on the second, and then raises RuntimeError on the third call. Which mock setup achieves this?
Attempts:
2 left
💡 Hint
side_effect list can include values or callables; exceptions must be raised, not returned.
✗ Incorrect
Option B uses a lambda that raises RuntimeError on call, so the third call raises the exception. Option A returns the exception object instead of raising it. Option C uses the exception class, which is not raised. Option D sets return_value to a list, so all calls return the list.