Challenge - 5 Problems
Mocking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of MagicMock call count
What is the output of the following pytest code snippet?
PyTest
from unittest.mock import MagicMock def test_func(): mock_obj = MagicMock() mock_obj.method() mock_obj.method() return mock_obj.method.call_count result = test_func() print(result)
Attempts:
2 left
💡 Hint
MagicMock tracks how many times a mocked method is called.
✗ Incorrect
The MagicMock object tracks calls to its methods. Calling mock_obj.method() twice increments call_count to 2.
❓ assertion
intermediate2:00remaining
Correct assertion for mock call arguments
Which assertion correctly verifies that a MagicMock method was called with arguments (5, 10)?
PyTest
from unittest.mock import MagicMock mock_obj = MagicMock() mock_obj.method(5, 10)
Attempts:
2 left
💡 Hint
Use the built-in MagicMock method to check call arguments.
✗ Incorrect
assert_called_with is the correct method to assert the last call had specific arguments. called_with does not exist. call_args is a tuple with args and kwargs, so direct comparison to (5, 10) fails. call_args_list contains call objects, not tuples.
🔧 Debug
advanced2:00remaining
Identify the error in mocking a class method
What error will this pytest code raise when run?
PyTest
from unittest.mock import MagicMock class MyClass: def greet(self): return 'Hello' mock_instance = MagicMock(spec=MyClass) mock_instance.greet.return_value = 'Hi' print(mock_instance.greet()) print(mock_instance.nonexistent())
Attempts:
2 left
💡 Hint
Spec limits attributes to those on the original class.
✗ Incorrect
Using spec=MyClass restricts MagicMock to only attributes of MyClass. Calling mock_instance.nonexistent() raises AttributeError because 'nonexistent' is not defined in MyClass.
🧠 Conceptual
advanced2:00remaining
Purpose of using spec in MagicMock
Why is it useful to pass the 'spec' argument when creating a MagicMock object?
Attempts:
2 left
💡 Hint
Think about how spec helps catch mistakes in tests.
✗ Incorrect
Passing spec limits the mock to only have attributes and methods of the original object, preventing typos and incorrect usage in tests.
❓ framework
expert2:00remaining
Mock patching order and effect in pytest
Given the following pytest test with patch decorators, what will be the output printed?
PyTest
from unittest.mock import patch def get_data(): return 'real data' def process(): return get_data() @patch('__main__.get_data', return_value='mocked data 1') @patch('__main__.get_data', return_value='mocked data 2') def test_process(mock2, mock1): print(process()) test_process()
Attempts:
2 left
💡 Hint
Remember the order of patch decorators and which mock is applied last.
✗ Incorrect
The outer patch decorator is applied last and overrides the inner one. The first argument mock2 corresponds to the inner patch, and mock1 to the outer patch, which returns 'mocked data 1'. So process() returns 'mocked data 1'.