Skip to main content

aimock-pytest

pytest fixtures for aimock — mock LLM APIs, MCP tools, A2A agents, vector databases, and more.

Install

# From PyPI (once published):
pip install aimock-pytest

# Local install from a repo checkout:
pip install ./packages/aimock-pytest

Requires: Node.js >= 20 on PATH (or set AIMOCK_NODE_PATH).

Quick Start

The plugin auto-registers two fixtures: aimock (function-scoped) and aimock_session (session-scoped).

def test_hello(aimock):
    import requests

    # Set up a fixture
    aimock.on_message("hello", {"content": "Hi there!"})

    # Point your SDK at aimock
    r = requests.post(
        f"{aimock.base_url}/v1/chat/completions",
        json={
            "model": "gpt-4",
            "messages": [{"role": "user", "content": "hello"}],
        },
    )
    assert r.json()["choices"][0]["message"]["content"] == "Hi there!"

Fixtures

Fixture Scope Description
aimock function Fresh server per test
aimock_session session Shared server across all tests

Server API

# Add fixtures
aimock.on_message("pattern", {"content": "response"})
aimock.on_embedding("pattern", {"embedding": [0.1, 0.2]})
aimock.on_system_message("name=Atai", {"content": "..."}, user_message="who am I")
# Array form: all substrings must appear in the joined system text (AND)
aimock.on_system_message(["name=Atai", "tz=PST"], {"content": "..."})
aimock.add_fixture(match={...}, response={...}, chunkSize=10, latency=50)
# Ordered blocks: stream a tool call before text (tool-first / interleaved).
# A blocks-only response is first-class — see /fixtures#ordered-blocks
aimock.add_fixture(match={...}, response={"blocks": [
    {"type": "toolCall", "name": "get_weather", "arguments": {"city": "SF"}},
    {"type": "text", "text": "Here is the weather."},
]})
aimock.load_fixtures("path/to/fixtures.json")

# Inspect
aimock.get_journal()       # list of all recorded requests
aimock.get_last_request()  # most recent request or None

# Error injection
aimock.next_error(429, {"message": "Rate limited"})

# Reset
aimock.clear_fixtures()    # remove all fixtures
aimock.reset_fixtures()    # clear fixtures + generation state (and journal)
aimock.reset_journal()     # clear only the request journal (fixtures preserved)
aimock.reset()             # alias for reset_fixtures()

CLI Options

--aimock-node PATH       Path to node binary
--aimock-version VER     aimock npm version (default: 1.38.0)
--aimock-api-key KEY     Inbound API key for the aimock child process

API-key validation

Pass pytest --aimock-api-key test-key to protect the aimock child. The helper sends this key on all control API calls, and the child receives it through AIMOCK_API_KEYS, never through process arguments. Direct client calls must use Authorization: Bearer test-key. For direct construction, use AIMockServer(node_manager, api_key="test-key").

Environment Variables

Variable Description
AIMOCK_NODE_PATH Path to node binary
AIMOCK_CACHE_DIR Override cache directory (default: ~/.cache/aimock)

Development

Prerequisites

  • Node.js >= 20
  • Python >= 3.10
  • pnpm

Running tests locally

Build the npm package first, then point AIMOCK_CLI_PATH at the local build:

pnpm install && pnpm run build
AIMOCK_CLI_PATH=../../dist/cli.js pytest tests/ -v

If you install the test dependencies and run from the packages/aimock-pytest/ directory, conftest.py will auto-detect the local build so you can omit the env var:

pip install ./packages/aimock-pytest[test]
cd packages/aimock-pytest
pytest tests/ -v

How CI works

The test-pytest.yml workflow:

  1. Checks out the repo
  2. Builds the TypeScript package (pnpm run build)
  3. Sets AIMOCK_CLI_PATH to the local dist/cli.js
  4. Installs aimock-pytest[test] and runs pytest

Tests run across a matrix of Python 3.10--3.13 and Node 20/22.

The Release workflow publishes aimock-pytest to PyPI after its npm publish job succeeds. Its PyPI job verifies that the AIMOCK_VERSION pin exists on npm before building a wheel, so npm publication completes before the corresponding aimock-pytest release.

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aimock_pytest-0.5.2.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aimock_pytest-0.5.2-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file aimock_pytest-0.5.2.tar.gz.

File metadata

  • Download URL: aimock_pytest-0.5.2.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for aimock_pytest-0.5.2.tar.gz
Algorithm Hash digest
SHA256 28ed44f7f7cc9d2321c7b8b761a29f1537d4d8598063327ba2a1d4ac46034b24
MD5 89dabe7475dcf401da7834bdc61d46ee
BLAKE2b-256 723e7017181cb945bf8886a2fdca256f0130b2ca8d2bec8e88db90626bb24050

See more details on using hashes here.

Provenance

The following attestation bundles were made for aimock_pytest-0.5.2.tar.gz:

Publisher: publish-release.yml on CopilotKit/aimock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aimock_pytest-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: aimock_pytest-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for aimock_pytest-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 29ac7b3a07d9c73955ab08f75736ebcf728134eceb5264b988e8e933f5ee2c35
MD5 5f292c2ba123279efa07f8c9f25ab118
BLAKE2b-256 20e7cd5be4f171e02b38897aa37fc85396a8edcf7970017a35ca2a464a81fe4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for aimock_pytest-0.5.2-py3-none-any.whl:

Publisher: publish-release.yml on CopilotKit/aimock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page