0
0
PyTesttesting~20 mins

Mock return values and side effects in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mock Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
Amocked data
Breal data
CNone
DTypeError
Attempts:
2 left
💡 Hint
The mock object returns what you set as return_value when called.
assertion
intermediate
2: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'))
A
with pytest.raises(ValueError):
    mock_func()
Bassert mock_func() == ValueError
Cassert mock_func.side_effect == ValueError
Dassert mock_func() raises ValueError
Attempts:
2 left
💡 Hint
Use pytest.raises context manager to check exceptions.
🔧 Debug
advanced
2: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())
AStopIteration because side_effect list is exhausted
BIndexError because list index out of range
CTypeError because side_effect is not callable
DNo error, prints 1, 2, None
Attempts:
2 left
💡 Hint
side_effect list acts like an iterator and raises StopIteration when empty.
🧠 Conceptual
advanced
2: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.
Areturn_value sets the value returned by the mock; side_effect can be a function, exception, or iterable to control behavior
Breturn_value raises exceptions; side_effect returns fixed values
Creturn_value is used only for async mocks; side_effect only for sync mocks
Dreturn_value and side_effect are interchangeable and do the same thing
Attempts:
2 left
💡 Hint
Think about how you can simulate different behaviors with side_effect.
framework
expert
3: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?
Amock_func = Mock(side_effect=[10, 20, RuntimeError('fail')])
Bmock_func = Mock(side_effect=[10, 20, lambda: (_ for _ in ()).throw(RuntimeError('fail'))])
Cmock_func = Mock(side_effect=[10, 20, RuntimeError])
Dmock_func = Mock(return_value=[10, 20, RuntimeError('fail')])
Attempts:
2 left
💡 Hint
side_effect list can include values or callables; exceptions must be raised, not returned.