Bird
Raised Fist0
PyTesttesting~20 mins

Mock call assertions in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 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.

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

  1. 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.
  2. 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.
  3. Final Answer:

    It checks if the last call to the mock was with specific arguments. -> Option D
  4. 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

  1. Step 1: Identify the correct assertion method

    The method to check a single call with specific arguments is assert_called_once_with.
  2. Step 2: Verify syntax correctness

    The syntax requires calling this method on the mock object with the expected arguments inside parentheses.
  3. Final Answer:

    mock.assert_called_once_with(5, 'test') -> Option A
  4. 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
3. Given the code:
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?
medium
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

  1. Step 1: Understand assert_called_with behavior

    This assertion checks that the last call to the mock was with the specified arguments.
  2. 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.
  3. Final Answer:

    The assertion passes because the last call was with (3, 4). -> Option A
  4. 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

  1. Step 1: Check method usage

    assert_called_once checks the call count but does not accept arguments to check call parameters.
  2. Step 2: Correct method for argument check

    To check the call arguments and count, assert_called_once_with should be used instead.
  3. Final Answer:

    assert_called_once does not accept arguments; use assert_called_once_with instead. -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    mock.assert_has_calls([call(1, 'a'), call(2, 'b'), call(3, 'c')], any_order=False) -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard