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") # also loads the file's mcpFakes
# 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, nothing else
aimock.reset() # full reset: fixtures, journal entries + match-counts,
# video/fal job state, Gemini counters
aimock.reset_journal() # clear only the request journal (fixtures preserved)
aimock.reset_fixtures() # alias for reset() — a full reset, despite the name
MCP fakes
load_fixtures posts a file's mcpFakes key (see MCP scenario fakes) next to its fixtures. A file may hold only mcpFakes. reset(), reset_fixtures() and clear_fixtures() also unload the fakes.
When the server rejects a file with HTTP 400, load_fixtures raises requests.HTTPError with the message aimock rejected fixtures from <path>: <error>, followed by one line for each item of details. When the file has mcpFakes and the aimock server is a release without MCP fakes, load_fixtures raises RuntimeError (aimock server too old for mcpFakes: ...) and adds nothing from the file.
Per-test fake reports
These need aimock 1.45.0 or later.
aimock.fakes_for(test_id=None, context=None, mount="/mcp")returns aFakesTargetwithtest_id,mcp_url(the mount URL with?testId=, and&context=when given) andheaders(X-Test-Id, andX-AIMock-Contextwhen given). Give the URL or the headers to your MCP client.aimock.fakes_report(test_id=None, context=None)returns the fakes report fromGET /__aimock/mcp/fakes/report:ok,served,unconsumed,failures,unfaked,sharedUnconsumedandevicted.aimock.assert_fakes_report(report=None, *, test_id=None, context=None, fail_on_unfaked=False)raisesAssertionErrorwhen the report is not ok: a fake error (such asMCP_FAKE_MISMATCH), an unused entry, or evicted state. Withfail_on_unfaked=True, a call that no fake answered also fails. With noreport, it reads the report first. The message has the same lines as the JavaScriptassertFakesReport.- With the function-scoped
aimockfixture, aNonetest id is the current test'srequest.node.nodeid, for exampletests/test_weather.py::test_seattle. Scope yourmcpFakesblock to that id. The default is used only by these three methods; LLM and control requests are not tagged. Withaimock_sessionthere is no current test, so these methods raiseValueErrorunless you passtest_id.
CLI Options
--aimock-node PATH Path to node binary
--aimock-version VER aimock npm version (default: 1.45.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
Run these commands from the repository root with Python 3.10 or later and Node.js 20.15.0 or later. The example uses Python 3.12.
Build the local CLI and install the Python test dependencies in a virtual environment:
pnpm install --frozen-lockfile
pnpm run build
python3.12 -m venv .venv
. .venv/bin/activate
python -m pip install --require-hashes -r .github/requirements/hatchling.txt
python -m pip install --no-build-isolation -e "./packages/aimock-pytest[test]"
export AIMOCK_CLI_PATH="$PWD/dist/cli.js"
python -m pytest packages/aimock-pytest/tests/ -v
The test extra installs pytest and the OpenAI and Anthropic Python SDKs.
AIMOCK_CLI_PATH selects the local build and bypasses the npm package download.
Keep the complete dist/ directory from that build, because the CLI imports other
build files. Rebuild after changes to the TypeScript source.
The test conftest.py also detects the repository's dist/cli.js when
AIMOCK_CLI_PATH is unset. An explicit absolute path selects the intended candidate
even when another build exists. To use a different checkout, set this variable to
that checkout's built dist/cli.js before running pytest.
To run only the model-misbehavior SDK and control tests:
python -m pytest \
packages/aimock-pytest/tests/test_misbehavior_openai.py \
packages/aimock-pytest/tests/test_misbehavior_anthropic.py \
packages/aimock-pytest/tests/test_misbehavior_controls.py -v
These tests send real SDK requests to the local aimock server. They use local test API keys and disable SDK retries. No live provider credentials are required. The tests check SDK parsing, streamed events, and control behavior against the selected CLI build. A test failure can expose a provider implementation gap in that candidate.
How CI works
The test-pytest.yml workflow:
- Checks out the repo
- Builds the TypeScript package (
pnpm run build) - Sets
AIMOCK_CLI_PATHto the localdist/cli.js - Installs
aimock-pytest[test]and runspytest
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
Metadata
Release files for aimock-pytest 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| aimock_pytest-0.7.0.tar.gz | 38.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aimock_pytest-0.7.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 56.2 kB
Release files / aimock_pytest-0.7.0.tar.gz
| Download URL | aimock_pytest-0.7.0.tar.gz |
|---|---|
| Size | 38.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a1f2e0a5da0292937883e0eecadcda19fe735db5375b6b6d8f9f3ce8868d3c7a
|
|
BLAKE2b-256 checksum How to use checksums |
857c8745f5444a86fb7a9000a05329be6c8d03be705fa85d2605713b05890027
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 10, 2026.
Transparency logRelease files / aimock_pytest-0.7.0-py3-none-any.whl
| Download URL | aimock_pytest-0.7.0-py3-none-any.whl |
|---|---|
| Size | 18.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
23b0b1d622e89ee86779e3b3208e99fa89d633f9f365e24e79630057a44cc951
|
|
BLAKE2b-256 checksum How to use checksums |
ab945a4ac0e1629f9967a35995812ac68f3c846df2bb35bb6fca64b0b1f3bba2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 10, 2026.
Transparency log