0
0
PyTesttesting~20 mins

Mock and MagicMock in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mocking Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A2
B1
C0
DRaises AttributeError
Attempts:
2 left
💡 Hint
MagicMock tracks how many times a mocked method is called.
assertion
intermediate
2: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)
Aassert mock_obj.method.assert_called_with(5, 10)
Bassert mock_obj.method.called_with(5, 10)
Cassert mock_obj.method.call_args == (5, 10)
Dassert mock_obj.method.call_args_list == [(5, 10)]
Attempts:
2 left
💡 Hint
Use the built-in MagicMock method to check call arguments.
🔧 Debug
advanced
2: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())
ANameError: name 'nonexistent' is not defined
BTypeError: 'MagicMock' object is not callable
CNo error, prints 'Hi' and then another MagicMock object
DAttributeError: 'MagicMock' object has no attribute 'nonexistent'
Attempts:
2 left
💡 Hint
Spec limits attributes to those on the original class.
🧠 Conceptual
advanced
2:00remaining
Purpose of using spec in MagicMock
Why is it useful to pass the 'spec' argument when creating a MagicMock object?
AIt makes the mock object behave exactly like the original object including internal state
BIt automatically records all calls and arguments without needing extra setup
CIt restricts the mock to only allow attributes and methods that exist on the original object
DIt converts the mock into a real instance of the original class
Attempts:
2 left
💡 Hint
Think about how spec helps catch mistakes in tests.
framework
expert
2: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()
ARaises RuntimeError due to patch conflict
Bmocked data 1
Creal data
Dmocked data 2
Attempts:
2 left
💡 Hint
Remember the order of patch decorators and which mock is applied last.