Skip to main content

pytest fixtures for aimock — mock LLM APIs, MCP, A2A, vector DBs

Project description

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.add_fixture(match={...}, response={...}, chunkSize=10, latency=50)
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()             # clear fixtures + journal

CLI Options

--aimock-node PATH       Path to node binary
--aimock-version VER     aimock npm version (default: 1.7.0)

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 publish-pytest.yml workflow publishes to PyPI on pushes to main when the version in pyproject.toml has not already been published.

License

MIT

Project details


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.3.0.tar.gz (8.2 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.3.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aimock_pytest-0.3.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for aimock_pytest-0.3.0.tar.gz
Algorithm Hash digest
SHA256 56e38499e651649fa8646bbb84545bb5e2810916519d826f5ae1b183e9739248
MD5 dbec2c93146f325078c352bc3e4bc641
BLAKE2b-256 055af279d6384ff79adb91508cda3296c66888fdfe2f79cdc48d5d8de80f822c

See more details on using hashes here.

Provenance

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

Publisher: publish-pytest.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.3.0-py3-none-any.whl.

File metadata

  • Download URL: aimock_pytest-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for aimock_pytest-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f5b9b7b64329211e1966ec2cea291ae06452f2f90450922f9209e3182153906f
MD5 f3ea67f62ff8ce9205bc1439e6c76828
BLAKE2b-256 4e438284f4eccd1fe60c0ba25bc331b5e55eba2d826cb418c5216dbe1ee38aac

See more details on using hashes here.

Provenance

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

Publisher: publish-pytest.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