0
0
PyTesttesting~15 mins

pytest-mock for enhanced mocking - Build an Automation Script

Choose your learning style9 modes available
Mock a function call using pytest-mock and verify it was called with expected arguments
Preconditions (2)
Step 1: Use pytest-mock's 'mocker' fixture to mock 'fetch_data' function
Step 2: Set the mock to return a fixed dictionary {'id': 1, 'value': 'test'}
Step 3: Call the function under test that uses 'fetch_data'
Step 4: Verify that 'fetch_data' was called exactly once
Step 5: Verify that 'fetch_data' was called with no arguments
Step 6: Assert that the function under test returns the mocked data
✅ Expected Result: The test passes confirming the mock was called correctly and the function under test returns the mocked data
Automation Requirements - pytest with pytest-mock plugin
Assertions Needed:
'fetch_data' was called once
'fetch_data' was called with expected arguments
Function under test returns mocked data
Best Practices:
Use pytest fixtures for setup
Use mocker.patch to replace functions
Avoid hardcoding side effects inside the test
Keep tests isolated and independent
Automated Solution
PyTest
import pytest
from data_source import fetch_data

# Function under test that calls fetch_data

def process_data():
    data = fetch_data()
    # Imagine some processing here
    return data


def test_process_data_calls_fetch_data_once_and_returns_mocked_data(mocker):
    # Mock fetch_data to return fixed data
    mock_fetch = mocker.patch('data_source.fetch_data', return_value={'id': 1, 'value': 'test'})

    result = process_data()

    # Assert fetch_data was called once
    mock_fetch.assert_called_once()

    # Assert fetch_data was called with no arguments
    mock_fetch.assert_called_with()

    # Assert the function under test returns the mocked data
    assert result == {'id': 1, 'value': 'test'}

The test uses the mocker fixture from pytest-mock to replace the fetch_data function with a mock that returns a fixed dictionary.

We call the process_data function which internally calls fetch_data. The mock ensures no real API call happens.

Assertions check that fetch_data was called exactly once and with no arguments, matching the manual test case steps.

Finally, we assert that process_data returns the mocked data, confirming the mock worked as expected.

Common Mistakes - 3 Pitfalls
{'mistake': "Not using the 'mocker' fixture and trying to patch directly with 'unittest.mock.patch'", 'why_bad': "This can cause issues with pytest's test discovery and fixture management.", 'correct_approach': "Always use the 'mocker' fixture provided by pytest-mock for patching in pytest tests."}
{'mistake': 'Mocking the wrong import path of the function', 'why_bad': 'Mocking the function where it is defined instead of where it is used will not replace the call in the function under test.', 'correct_approach': "Patch the function at the import location used by the function under test (e.g., 'data_source.fetch_data')."}
{'mistake': 'Not asserting the mock call count or arguments', 'why_bad': 'Without these assertions, the test might pass even if the function was not called or called incorrectly.', 'correct_approach': "Use 'assert_called_once()' and 'assert_called_with()' to verify correct usage."}
Bonus Challenge

Now add data-driven testing with 3 different mocked return values for 'fetch_data' and verify 'process_data' returns each correctly

Show Hint