0
0
PyTesttesting~10 mins

Mock call assertions in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if a mocked function is called with the expected arguments. It verifies that the function was called exactly once with the correct parameters.

Test Code - pytest
PyTest
from unittest.mock import Mock
import pytest

def test_mock_call():
    mock_func = Mock()
    # Call the mock function with specific arguments
    mock_func('hello', key='value')
    # Assert the mock was called once with the expected arguments
    mock_func.assert_called_once_with('hello', key='value')
Execution Trace - 3 Steps
StepActionSystem StateAssertionResult
1Create a mock function objectmock_func is a Mock object with no calls recorded-PASS
2Call mock_func with arguments ('hello', key='value')mock_func records one call with arguments ('hello', key='value')-PASS
3Assert mock_func was called once with ('hello', key='value')mock_func call history matches expected argumentsmock_func.assert_called_once_with('hello', key='value') verifies call count and argumentsPASS
Failure Scenario
Failing Condition: mock_func was not called exactly once with the expected arguments
Execution Trace Quiz - 3 Questions
Test your understanding
What does assert_called_once_with check in this test?
AThat the mock was never called
BThat the mock was called exactly once with the specified arguments
CThat the mock was called multiple times with any arguments
DThat the mock returned a specific value
Key Result
Always verify both the number of calls and the exact arguments when asserting mock calls to ensure your code interacts correctly with dependencies.