← ClaudeAtlas

pydantic-ai-testinglisted

Test PydanticAI agents using TestModel, FunctionModel, VCR cassettes, and inline snapshots. Use when writing unit tests, mocking LLM responses, or recording API interactions.
existential-birds/beagle · ★ 61 · AI & Automation · score 84
Install: claude install-skill existential-birds/beagle
# Testing PydanticAI Agents ## TestModel (Deterministic Testing) Use `TestModel` for tests without API calls: ```python import pytest from pydantic_ai import Agent from pydantic_ai.models.test import TestModel def test_agent_basic(): agent = Agent('openai:gpt-4o') # Override with TestModel for testing result = agent.run_sync('Hello', model=TestModel()) # TestModel generates deterministic output based on output_type assert isinstance(result.output, str) ``` ## TestModel Configuration ```python from pydantic_ai.models.test import TestModel # Custom text output model = TestModel(custom_output_text='Custom response') result = agent.run_sync('Hello', model=model) assert result.output == 'Custom response' # Custom structured output (for output_type agents) from pydantic import BaseModel class Response(BaseModel): message: str score: int agent = Agent('openai:gpt-4o', output_type=Response) model = TestModel(custom_output_args={'message': 'Test', 'score': 42}) result = agent.run_sync('Hello', model=model) assert result.output.message == 'Test' # Seed for reproducible random output model = TestModel(seed=42) # Force tool calls model = TestModel(call_tools=['my_tool', 'another_tool']) ``` ## Override Context Manager ```python from pydantic_ai import Agent from pydantic_ai.models.test import TestModel agent = Agent('openai:gpt-4o', deps_type=MyDeps) def test_with_override(): mock_deps = MyDeps(db=MockDB()) with agent.override(model=