A FastAPI service that proxies questions to a local LLM (Ollama), paired with a pytest suite (23 tests, 100% coverage) that demonstrates how to test a non-deterministic system without relying on a live model.
Live reports: test report · coverage
Built as project 1 of 5 exploring AI/LLM testing. A writeup is in progress.
respx to mock outgoing httpx calls so error paths run without
a live dependencyASGITransport - no live HTTP
server, no port juggling-m "not ollama") and a full integration slice on demandxfail(strict=False) so they flip
to passing the day the classifier is upgradedxfailrespx so they run without a live LLMRequires Python 3.10+.
# Setup
python3 -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
# Fast suite - no Ollama needed, ~1 second
pytest -m "not ollama"
For the full suite (integration tests against a real LLM), start Ollama in a separate terminal tab:
ollama serve
Then, in this terminal:
ollama pull llama3.2
pytest --html=reports/report.html --self-contained-html \
--cov=app --cov-report=html:reports/htmlcov \
--cov-report=term-missing --durations=10
Sample reports are committed and hosted live on GitHub Pages so you can preview without running:
Open them after a run:
open reports/report.html # pytest-html report
open reports/htmlcov/index.html # coverage report
(Linux: xdg-open; Windows: start.)
Start the FastAPI server with uvicorn (requires Ollama running for /ask):
uvicorn app.main:app --reload
Then visit:
/ask from the browser)| Marker | Runtime | Meaning |
|---|---|---|
ollama |
1–60 s | Requires a real Ollama at localhost:11434 |
mocked |
<50 ms | Uses respx to mock Ollama (error-path tests) |
Select with pytest -m "ollama", pytest -m mocked, or pytest -m "not ollama".
llm-api-testing/
├── app/
│ └── main.py # FastAPI app + Ollama client + structured logging
├── tests/
│ └── test_api.py # 23 tests, sectioned by pattern (app / mocked / integration)
├── reports/ # sample HTML reports, committed for preview
│ ├── report.html # pytest-html report
│ └── htmlcov/ # coverage-html report
├── pyproject.toml # pytest config + marker registration
├── requirements.txt # runtime + test dependencies
└── README.md
FastAPI · httpx · pytest · pytest-asyncio · pytest-html · pytest-cov · respx · Ollama (llama3.2)