← ClaudeAtlas

writing-testslisted

Use when adding or modifying tests in this repo, when a test hits the network or the real user data directory, when a monkeypatch has no effect, or before running pytest here.
ontech7/ollama-usage · ★ 0 · AI & Automation · score 70
Install: claude install-skill ontech7/ollama-usage
# Writing tests ## Overview The suite is pure `pytest` + `typer.testing.CliRunner`, and it must stay **hermetic**: no network, no real home directory, no real cookie. Two project conventions make that work, and both are easy to break by accident. Run: `uv sync --dev` once, then `uv run pytest` (this is exactly what CI does). ## Convention 1 — module-reference imports are a test seam `cli.py` and `watch.py` deliberately import *modules*, not symbols: ```python import ollama_usage.fetch as fetch # ✅ monkeypatchable import ollama_usage.session as session # NOT: from ollama_usage.fetch import fetch_settings ``` The attribute is looked up at call time, so `monkeypatch.setattr(fetch_mod, "fetch_settings", fake)` takes effect. Rewriting these to `from … import …` makes every monkeypatch a silent no-op: the tests still pass, but they exercise the real network path. The comment at the top of `watch.py` says so — leave the imports alone. ## Convention 2 — isolation fixtures ```python monkeypatch.setenv("OLLAMA_USAGE_DATA_DIR", str(tmp_path)) # session/cache/state monkeypatch.setenv("OLLAMA_USAGE_SETTINGS_URL", "https://example.com/settings") ``` `get_paths()` reads those env vars every call, so this redirects `session.json`, `cache.json` and `state.json` into `tmp_path`. **Never** let a test write to the real platformdirs data dir. `tests/conftest.py` has an **autouse** fixture setting `OLLAMA_USAGE_NO_UPDATE_CHECK=1`, so no test hits GitHub by default. A test tha