Skip to main content

pytest-mockllm 0.3.1: Fixture-Scoped Mocks with SDK Contract Coverage

Test supported OpenAI and Anthropic SDK paths locally, with clear interception boundaries and honest token estimates.

3 min read
pytest-mockllm 0.3.1: Fixture-Scoped Mocks with SDK Contract Coverage
On this page

Release checked September 5, 2026
This article now reflects v0.3.1 and the documented behavior of that project. Existing URLs are preserved so older links continue to work.

A mock should give deterministic inputs to application tests without pretending to reproduce every provider behavior. Version 0.3.1 adds OpenAI and Anthropic SDK contract coverage and hardens provider-fixture interception. The important boundary is fixture scope: installing the plugin alone does not prevent live network requests.

Use the provider fixture explicitly

bash
python -m pip install "pytest-mockllm[openai]==0.3.1"
python
def test_completion(mock_openai):
    from openai import OpenAI

    mock_openai.add_response('Ready for review')
    client = OpenAI(api_key='fixture-only')
    response = client.chat.completions.create(
        model='gpt-4o',  # Fixture model identifier; no live call
        messages=[{'role': 'user', 'content': 'Report the status'}],
    )
    assert response.choices[0].message.content == 'Ready for review'
    assert mock_openai.call_count == 1

The placeholder key satisfies the SDK constructor. The active fixture intercepts the supported call. Keep real credentials out of unit-test environments and use CI egress controls if the suite must never contact a provider.

Async uses the same pytest fixture

python
import asyncio

def test_async_completion(mock_openai):
    from openai import AsyncOpenAI

    mock_openai.add_response('Async result')
    async def run():
        async with AsyncOpenAI(api_key='fixture-only') as client:
            return await client.chat.completions.create(
                model='gpt-4o',
                messages=[{'role': 'user', 'content': 'Report the status'}],
            )
    response = asyncio.run(run())
    assert response.choices[0].message.content == 'Async result'

Know which interfaces are covered

FixtureSupported boundary
mock_openaiDocumented Chat Completions, Responses, embeddings, sync/async and streaming paths
mock_anthropicDocumented Messages and text/tool streaming paths
mock_geminiLegacy google-generativeai high-level entry points
mock_langchainDocumented ChatOpenAI-compatible and direct mock-model paths

OpenAI and Anthropic supported paths return official SDK response or event objects when the SDK is installed. Their active fixtures also reject unhandled base requests, including those from clients created before activation. Gemini and LangChain are not global network guards. The replacement google-genai SDK is not yet implemented by this plugin.

Token estimates are not provider invoices

Token and cost reporting is useful for comparison, but model-specific encoding, message overhead, and pricing affect the result. The old claim of greater than 99% accuracy and exact dashboard agreement has been removed. Treat totals as estimates and verify billing separately.

Recording and replay are unavailable

Recording and replay are currently unavailable. The auto, record, and replay modes fail before test code can reach a provider; they do not create or replay cassettes. Use deterministic provider fixtures instead. Redaction cannot guarantee that arbitrary data is safe to share. A successful mock test validates application behavior against the configured fixture, not the quality or safety of a live model.

Keep a separate, opt-in set of provider contract and live evaluation tests. That separation makes it clear whether a failure belongs to application logic, SDK compatibility, or actual model behavior.

Current project documentation

Dhiraj Das

About the Author

Dhiraj Das is an Automation Consultant with over a decade of experience building systems that expose failures, reduce flakiness, and make complex workflows repeatable. He applies that discipline to AI-agent validation, LLM testing, and postmortems.

He shares small open source utilities from real automation work, including: waitless (flaky tests), sb-stealth-wrapper (bot detection), selenium-teleport (state persistence), selenium-chatbot-test (AI chatbot testing), lumos-shadowdom (Shadow DOM), and visual-guard (visual regression).

Share this article: