pytest: one take per test
python/pytest-retake is a pytest plugin. Under Retake it tells
the proxy when each test begins and ends and whether it passed, and attributes each
request to its test, so every test case gets its own take.
examples/pytest_refund is a pytest suite for a Python
version of the refund agent, using the official anthropic SDK: a plain test, a
parametrized test, a refused refund and a streamed run.
In your own project, pip install pytest-retake installs the plugin and the retake
command (see Installation); then wrap your usual pytest command, e.g.
retake replay -- pytest tests/.
To try the example, record it (real, billed API calls) from the root of a clone of the
repository. uv run --with installs anthropic, pytest and the plugin from PyPI into a
temporary environment; any environment with those three works the same way:
ANTHROPIC_API_KEY=... ANTHROPIC_MODEL=claude-haiku-5-5 \
retake record --takes-dir examples/pytest_refund/takes \
-- uv run --with anthropic --with pytest --with pytest-retake \
pytest examples/pytest_refund
retake: recorded 15 exchange(s) to per-test takes in examples/pytest_refund/takes (...)
retake: 5 test take(s) recorded:
retake: test_refund.py/test_order_not_arrived.take: recorded 3 exchange(s)
retake: test_refund.py/test_refund_amount/case-all.take: recorded 3 exchange(s)
retake: test_refund.py/test_refund_amount/case-quarter.take: recorded 3 exchange(s)
retake: test_refund.py/test_refund_half.take: recorded 3 exchange(s)
retake: test_refund.py/test_refund_half_streamed.take: recorded 3 exchange(s)
(The exchange counts depend on what the model does.) Replay it with no key and no network, with the same model:
env -u ANTHROPIC_API_KEY ANTHROPIC_MODEL=claude-haiku-5-5 \
retake replay --takes-dir examples/pytest_refund/takes \
-- uv run --with anthropic --with pytest --with pytest-retake \
pytest examples/pytest_refund
With the plugin:
- Take names follow the pytest node ID, relative to the takes directory:
test_refund.py::test_refund_amount[quarter]istest_refund.py/test_refund_amount/case-quarter.take. IDs with unsafe characters, or too long, get a short hash.@pytest.mark.retake("some/name")sets a name explicitly. The full test ID is in the take’s header. - A test that fails while recording leaves its previous take in place; the new
recording is discarded.
--keep-failedkeeps it, marked"outcome":"failed"in the footer, and replay refuses such a take unless--allow-failedis given. - A test that passes without making any request while recording has its previous take removed (it would fail replay as unused); the summary lists it as removed.
- A skipped test (
skip,skipif,importorskip, or a skip in the middle of the test) never loses its take: while recording, whatever it recorded is discarded and its previous take kept; on replay, its take is not checked for unused exchanges. - On replay, a test that leaves recorded exchanges unused fails (it made fewer calls than were recorded), as does a test with no take.
- Requests made outside a test (session- or module-scoped fixtures, imports) go to
_session.take, which every recording run replaces. Tests that use IDs from it are re-recorded together; see below. - Without Retake (
RETAKE_URLunset) the plugin does nothing and warns once;RETAKE_REQUIRED=1makes that an error, e.g. in CI.
The plugin attaches the test ID by wrapping httpx2 and httpx (which the anthropic
SDK uses); see its README for how this works and its
limits.