Mock call assertions help check if a fake function was called correctly during a test. This makes sure your code talks to other parts as expected.
Mock call assertions in PyTest
mock_function.assert_called() mock_function.assert_called_once() mock_function.assert_called_with(arg1, arg2) mock_function.assert_called_once_with(arg1, arg2) mock_function.assert_any_call(arg1) mock_function.call_count
Use assert_called() to check if the mock was called at least once.
Use assert_called_with() to check the last call's arguments.
mock_func was called at least once.mock_func.assert_called()
mock_func was called exactly once with arguments 5 and 'test'.mock_func.assert_called_once_with(5, 'test')
mock_func was called at least once with argument 'hello'.mock_func.assert_any_call('hello')mock_func was called.print(mock_func.call_count)This test replaces the real greet function with a mock. It calls welcome_user with the mock and checks if the mock was called once with 'Alice'. It also prints how many times the mock was called.
from unittest.mock import Mock def greet(name): print(f"Hello, {name}!") def welcome_user(greet_func, user_name): greet_func(user_name) mock_greet = Mock() welcome_user(mock_greet, 'Alice') mock_greet.assert_called_once_with('Alice') print(f"Call count: {mock_greet.call_count}")
Mock call assertions help test interactions without running real code.
Always check the call arguments to catch mistakes early.
Use call_count to verify how many times a mock was used.
Mock call assertions check if and how mocks were called.
They help test code interactions safely and clearly.
Common assertions include assert_called_with and assert_called_once_with.
Practice
assert_called_with method check in pytest mocks?Solution
Step 1: Understand the purpose of
This method verifies that the last call to the mock was with the exact arguments provided.assert_called_withStep 2: Differentiate from other assertions
Unlikeassert_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 DQuick Check:
assert_called_with = last call with args [OK]
- Confusing assert_called_with with assert_called_once_with
- Thinking it checks call count exactly
- Assuming it checks return values
Solution
Step 1: Identify the correct assertion method
The method to check a single call with specific arguments isassert_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 AQuick Check:
assert_called_once_with = exactly one call with args [OK]
- 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
from unittest.mock import Mock mock = Mock() mock(1, 2) mock(3, 4) mock.assert_called_with(3, 4)
What will happen when the last line runs?
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 AQuick Check:
assert_called_with checks last call args [OK]
- Thinking assert_called_with checks all calls
- Confusing call order importance
- Expecting failure due to earlier calls
from unittest.mock import Mock mock = Mock() mock(10) mock.assert_called_once(10)
Solution
Step 1: Check method usage
assert_called_oncechecks 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_withshould be used instead.Final Answer:
assert_called_once does not accept arguments; use assert_called_once_with instead. -> Option CQuick Check:
assert_called_once no args; assert_called_once_with with args [OK]
- Passing arguments to assert_called_once
- Confusing call order
- Assuming assert_called_once checks arguments
(1, 'a'), (2, 'b'), (3, 'c')Which assertion correctly verifies all calls in order?
Solution
Step 1: Understand verifying multiple calls
To check multiple calls in order,assert_has_callswith a list of expected calls andany_order=Falseis used.Step 2: Analyze other options
mock.assert_called()only checks if it was called at least once without verifying arguments or order. Chainingmock.assert_called_with(1, 'a')andmock.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 BQuick Check:
assert_has_calls with any_order=False checks ordered calls [OK]
- Using assert_called_with for multiple calls
- Ignoring call order
- Not importing call from unittest.mock
Ready to prove it?
Pass the test and this pattern moves unknown
