A mock call assertion checks if a mocked function was called as expected during a test. It helps verify interactions without running real code.
Click to reveal answer
beginner
How do you assert that a mock was called exactly once in pytest?
Use mock_function.assert_called_once() to check the mock was called exactly one time.
Click to reveal answer
beginner
What does assert_called_with() do in mock assertions?
It checks that the mock was called with specific arguments at least once during the test.
Click to reveal answer
intermediate
Explain the difference between assert_called_once_with() and assert_called_with().
assert_called_once_with() checks the mock was called exactly once with the given arguments. assert_called_with() checks the mock was called at least once with those arguments, but can be multiple times.
Click to reveal answer
intermediate
How can you check the order of multiple calls to a mock?
Use mock_function.call_args_list to get a list of all calls in order, then assert each call's arguments.
Click to reveal answer
Which method asserts a mock was called exactly once with specific arguments?
Aassert_called_once()
Bassert_called_with()
Cassert_called_once_with()
Dassert_any_call()
✗ Incorrect
assert_called_once_with() checks the mock was called exactly once with the given arguments.
What does mock_function.assert_called() verify?
AMock was called at least once
BMock was called exactly once
CMock was never called
DMock was called with specific arguments
✗ Incorrect
assert_called() verifies the mock was called at least once, regardless of arguments.
How do you check if a mock was called with certain arguments at least once?
Aassert_called_once()
Bassert_called_with()
Cassert_any_call()
Dassert_not_called()
✗ Incorrect
assert_called_with() checks the mock was called at least once with the specified arguments.
Which attribute holds the list of all calls made to a mock?
Acall_args_list
Bcall_args
Ccalled
Dcall_count
✗ Incorrect
call_args_list contains all calls made to the mock in order.
What happens if assert_called_once() is used but the mock was called twice?
ATest raises a warning but passes
BTest passes
CTest skips
DTest fails with an assertion error
✗ Incorrect
assert_called_once() fails if the mock was called more than once.
Describe how to verify a mock function was called with specific arguments exactly once in pytest.
Think about the assertion method that combines call count and arguments.
You got /3 concepts.
Explain how to check the order and arguments of multiple calls made to a mock.
Look for a list that records all calls.
You got /3 concepts.
Practice
(1/5)
1. What does the assert_called_with method check in pytest mocks?
easy
A. It checks if the mock was called exactly twice.
B. It checks if the mock was never called.
C. It checks if the mock returned a specific value.
D. It checks if the last call to the mock was with specific arguments.
Solution
Step 1: Understand the purpose of assert_called_with
This method verifies that the last call to the mock was with the exact arguments provided.
Step 2: Differentiate from other assertions
Unlike assert_called_once_with, it does not require the call to be exactly once, just that the last call had those arguments.
Final Answer:
It checks if the last call to the mock was with specific arguments. -> Option D
Quick Check:
assert_called_with = last call with args [OK]
Hint: assert_called_with checks the last call's args [OK]
Common Mistakes:
Confusing assert_called_with with assert_called_once_with
Thinking it checks call count exactly
Assuming it checks return values
2. Which of the following is the correct syntax to assert a mock was called exactly once with arguments 5 and 'test'?
easy
A. mock.assert_called_once_with(5, 'test')
B. mock.assert_called_with(5, 'test')
C. mock.assert_called_once(5, 'test')
D. mock.assert_called(5, 'test')
Solution
Step 1: Identify the correct assertion method
The method to check a single call with specific arguments is assert_called_once_with.
Step 2: Verify syntax correctness
The syntax requires calling this method on the mock object with the expected arguments inside parentheses.
Final Answer:
mock.assert_called_once_with(5, 'test') -> Option A
Quick Check:
assert_called_once_with = exactly one call with args [OK]
Hint: Use assert_called_once_with(args) for exact single call [OK]
Common Mistakes:
Using assert_called_with instead of assert_called_once_with
Calling assert_called_once without _with
Passing arguments to assert_called which takes no args
A. The assertion passes because the last call was with (3, 4).
B. The assertion fails because the first call was with (1, 2).
C. The assertion fails because assert_called_with checks all calls.
D. The code raises a TypeError.
Solution
Step 1: Understand assert_called_with behavior
This assertion checks that the last call to the mock was with the specified arguments.
Step 2: Analyze the calls made
The mock was called twice: first with (1, 2), then with (3, 4). The last call matches the assertion arguments.
Final Answer:
The assertion passes because the last call was with (3, 4). -> Option A
Quick Check:
assert_called_with checks last call args [OK]
Hint: assert_called_with checks last call args by default [OK]
Common Mistakes:
Thinking assert_called_with checks all calls
Confusing call order importance
Expecting failure due to earlier calls
4. Identify the error in this test code snippet:
from unittest.mock import Mock
mock = Mock()
mock(10)
mock.assert_called_once(10)
medium
A. assert_called_once should be called before the mock call.
B. Mock object was never called, so assertion fails.
C. assert_called_once does not accept arguments; use assert_called_once_with instead.
D. The mock call should use keyword arguments.
Solution
Step 1: Check method usage
assert_called_once checks the call count but does not accept arguments to check call parameters.
Step 2: Correct method for argument check
To check the call arguments and count, assert_called_once_with should be used instead.
Final Answer:
assert_called_once does not accept arguments; use assert_called_once_with instead. -> Option C
Quick Check:
assert_called_once no args; assert_called_once_with with args [OK]
Hint: Use assert_called_once_with(args) to check call count and args [OK]
Common Mistakes:
Passing arguments to assert_called_once
Confusing call order
Assuming assert_called_once checks arguments
5. You have a mock that was called three times with arguments: (1, 'a'), (2, 'b'), (3, 'c') Which assertion correctly verifies all calls in order?
hard
A. mock.assert_called()
B. mock.assert_has_calls([call(1, 'a'), call(2, 'b'), call(3, 'c')], any_order=False)
C. mock.assert_called_once_with(3, 'c')
D. mock.assert_called_with(1, 'a') and mock.assert_called_with(3, 'c')
Solution
Step 1: Understand verifying multiple calls
To check multiple calls in order, assert_has_calls with a list of expected calls and any_order=False is used.
Step 2: Analyze other options
mock.assert_called() only checks if it was called at least once without verifying arguments or order. Chaining mock.assert_called_with(1, 'a') and mock.assert_called_with(3, 'c') checks some arguments but not all calls, order, or completeness. mock.assert_called_once_with(3, 'c') checks only that there was exactly one call with those arguments.
Final Answer:
mock.assert_has_calls([call(1, 'a'), call(2, 'b'), call(3, 'c')], any_order=False) -> Option B
Quick Check:
assert_has_calls with any_order=False checks ordered calls [OK]
Hint: Use assert_has_calls with any_order=False for ordered multiple calls [OK]
Common Mistakes:
Using assert_called_with for multiple calls
Ignoring call order
Not importing call from unittest.mock
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.