Patching the Wrong Target in pytest
Spot the classic mock.patch mistake that leaves the real function running during a pytest unit test.
Codepython
# app/service.py
from app.clients import fetch_user
def greet_user(user_id):
user = fetch_user(user_id)
return f"Hello, {user['name']}!"
# tests/test_service.py
from unittest.mock import patch
from app.service import greet_user
@patch("app.clients.fetch_user")
def test_greet_user(mock_fetch):
mock_fetch.return_value = {"name": "Ada"}
result = greet_user(42)
assert result == "Hello, Ada!"The test still calls the real network client instead of the mock. What is the bug?