0
0
PyTesttesting~20 mins

Mock call assertions in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mock Call Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2:00remaining
Identify the correct mock call assertion
Given a mock object mock_func called twice with arguments (1, 2) and (3, 4), which assertion correctly verifies these calls in order?
PyTest
from unittest.mock import Mock, call
mock_func = Mock()
mock_func(1, 2)
mock_func(3, 4)
A
mock_func.assert_called_once_with(1, 2)
mock_func.assert_called_once_with(3, 4)
B
mock_func.assert_called_with(1, 2)
mock_func.assert_called_with(3, 4)
Cmock_func.assert_has_calls([call(1, 2), call(3, 4)], any_order=False)
D
mock_func.assert_any_call(1, 2)
mock_func.assert_any_call(3, 4)
Attempts:
2 left
💡 Hint
Use assert_has_calls with any_order=False to check call order.
Predict Output
intermediate
1:30remaining
What is the output of this mock call count check?
What will be the value of mock_func.call_count after running this code?
PyTest
from unittest.mock import Mock
mock_func = Mock()
mock_func('a')
mock_func('b')
mock_func('c')
print(mock_func.call_count)
ARaises AttributeError
B1
C0
D3
Attempts:
2 left
💡 Hint
Each call to the mock increments call_count by one.
🔧 Debug
advanced
2:00remaining
Find the error in this mock call assertion
Why does this assertion fail even though the mock was called with (5, 6)?
PyTest
from unittest.mock import Mock
mock_func = Mock()
mock_func(5, 6)
mock_func.assert_called_with(6, 5)
AThe arguments order is wrong; assert_called_with expects exact order.
BThe mock was never called, so assertion fails.
Cassert_called_with requires keyword arguments, not positional.
Dassert_called_with only checks the first argument, so it fails.
Attempts:
2 left
💡 Hint
Check the order of arguments in the assertion versus the call.
framework
advanced
1:30remaining
Which pytest-mock assertion verifies a mock was never called?
Using pytest-mock, which assertion correctly checks that a mock function mock_func was never called?
Amock_func.assert_not_called()
Bmock_func.assert_called_once()
Cmock_func.assert_any_call()
Dmock_func.assert_called()
Attempts:
2 left
💡 Hint
Look for the assertion that confirms zero calls.
🧠 Conceptual
expert
2:30remaining
Understanding mock call argument matching
You have a mock called with keyword arguments mock_func(a=1, b=2). Which assertion will fail to verify this call correctly?
PyTest
from unittest.mock import Mock
mock_func = Mock()
mock_func(a=1, b=2)
Amock_func.assert_called_with(a=1, b=2)
Bmock_func.assert_called_with(1, 2)
Cmock_func.assert_called_with(b=2, a=1)
Dmock_func.assert_any_call(a=1, b=2)
Attempts:
2 left
💡 Hint
Check how positional vs keyword arguments are matched in assertions.