Pytest fixtures for the fakellm mock OpenAI/Anthropic server — spin up, reset, and assert with zero boilerplate.
Project description
pytest-fakellm
Pytest fixtures for fakellm, the mock OpenAI/Anthropic server. Spin up a server, get a clean state per test, and assert on what your code sent — with zero boilerplate.
pip install pytest-fakellm
Once installed, the fixtures are available automatically — no imports, no
conftest.py setup.
The point
Without the plugin, using fakellm in a test means starting the server, wiring a client to its URL, resetting state, and tearing it all down yourself, in every test. With the plugin, that becomes:
def test_agent_handles_search(fakellm):
fakellm.set_config_text("""
version: 1
rules:
- name: summarize
when: { messages_contain: "research" }
respond: { content: "Based on the search, I found what you were looking for." }
""")
result = run_my_agent(fakellm.openai_client(), prompt="Please research fakellm")
assert "found what you were looking for" in result
assert fakellm.request_count >= 1
The server starts once per session, state is reset before each test, and everything is torn down at the end. You never touch a port number or a subprocess.
Fixtures
| Fixture | What you get |
|---|---|
fakellm |
A FakellmServer handle with fresh conversation state for the test. |
fakellm_openai |
A ready openai.OpenAI client pointed at the (reset) server. |
fakellm_anthropic |
A ready anthropic.Anthropic client pointed at the (reset) server. |
FakellmServer handle
openai_client(**kwargs)/anthropic_client(**kwargs)— clients pointed at the server.openai_base_url/anthropic_base_url— raw URLs if you build your own client.set_config_text(yaml)— write rules inline and reload.load_rules(path)— load rules from a file and reload.reset()— clear conversation state (done for you between tests).reload()— re-read the config from disk.stats()/conversations()— the admin JSON, for assertions.request_count— convenience count of requests seen.
Configuration
Set a starting config file via the command line:
pytest --fakellm-config=tests/fixtures/rules.yaml
or in pyproject.toml / pytest.ini:
[tool.pytest.ini_options]
fakellm_config = "tests/fixtures/rules.yaml"
If you don't set one, a temporary empty config is created so set_config_text
and load_rules work immediately.
--fakellm-startup-timeout (default 10.0) controls how long the fixture waits
for the server to come up.
Client extras
openai_client() and anthropic_client() require the respective SDKs. Install
what you need:
pip install "pytest-fakellm[openai]" # adds openai
pip install "pytest-fakellm[anthropic]" # adds anthropic
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_fakellm-0.1.0.tar.gz.
File metadata
- Download URL: pytest_fakellm-0.1.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b58bb6b17c6404da657c2f8a9a92e985241fe2f452b63880d6446e5ed89453ca
|
|
| MD5 |
5865b39605fe64af82a126bbb0871365
|
|
| BLAKE2b-256 |
2be17577bec1503c99fb4f598a66c3b60b693635b629953d089320e709b97d8f
|
File details
Details for the file pytest_fakellm-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pytest_fakellm-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
320570ea03332c05dd497cd4ec2746ca00d82c1eee0c32d55caf57f242758105
|
|
| MD5 |
2ef9963e9d709dc585544a4a96615a90
|
|
| BLAKE2b-256 |
8db268a6cb04045f994563e7d111309643b942df8babd7731a54d6a75c06dbfb
|