Mocking LLM Calls in Tests
unittest.mock, pytest fixtures, and recording/replaying LLM responses.
What Is Mocking?
Mocking replaces a real function or object with a fake version that returns controlled responses. In agent testing, we mock LLM API calls so tests run instantly, cost nothing, and produce predictable results.
Python's unittest.mock module is the standard tool for this.
unittest.mock.patch() Basics
unittest.mock.patch(target) temporarily replaces the named object for the duration of a test. The target is a dotted string pointing to the object as it is imported in the module under test.
from unittest.mock import patch, MagicMock
# The function under test calls openai.chat.completions.create
# We patch it so no real API call is made
def ask_llm(question: str) -> str:
import openai
client = openai.OpenAI(api_key='test')
resp = client.chat.completions.create(
model='gpt-4o-mini',
messages=[{'role': 'user', 'content': question}]
)
return resp.choices[0].message.content
with patch('openai.OpenAI') as mock_client_class:
mock_instance = MagicMock()
mock_client_class.return_value = mock_instance
mock_instance.chat.completions.create.return_value = MagicMock(
choices=[MagicMock(message=MagicMock(content='Paris'))]
)
result = ask_llm('Capital of France?')
print(result) # 'Paris' — no API call madeAll lessons in this course
- Why Testing Agents Is Different
- Mocking LLM Calls in Tests
- Assertion-Based Agent Testing
- Integration Tests for Agent Pipelines