pytest-retake
pytest plugin for Retake. Under retake record or
retake replay it gives every test case its own take file, keeps the previous take when
a test fails, and fails a replayed test that makes fewer calls than were recorded.
pip install pytest-retake # also installs the `retake` binary (the retake-cli wheel)
retake record -- pytest tests/ # real API, writes tests/takes/<test path>.take
retake replay -- pytest tests/ # no network, no API key
Without Retake (RETAKE_URL unset) the plugin does nothing and warns once. Set
RETAKE_REQUIRED=1 (e.g. in CI) to make that an error, so a misconfigured job cannot run
live, paid tests.
What it does
- At start-up it calls Retake's
helloand picks the highest control protocol version both sides speak (no common version is an error). - Before each test's setup it sends
test/beginwith the pytest node ID (tests/test_refund.py::test_partial[half]). After teardown it sendstest/endwith the outcome and waits for the acknowledgement: up to Retake's in-flight timeout plus 10 s. The outcome isfailedfor any failing phase or an expected failure (xfail),skippedfor a skip (skip,skipif,importorskip, orpytest.skip()in setup or in the test), andpassedotherwise, including an unexpected pass (xpass). - A skipped 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.
- If Retake reports a problem at the end (unused recorded exchanges, requests without a recorded response, a refused recording), the test is marked failed with the reason.
- Requests to Retake's proxy made during a test carry the
x-retake-testheader (see below). Requests outside tests (session fixtures, module-level calls) carry none and are recorded in_session.take, as are requests made while fixtures wider than a function are set up or torn down.
Take names follow the test ID: tests/test_refund.py::test_partial[half] becomes
tests/takes/tests/test_refund.py/test_partial/case-half.take. Unsafe or long IDs get a
short hash.
Per-test settings
@pytest.mark.retake("refund/partial-half") # explicit take name (no .take)
@pytest.mark.retake(keep_failed=True) # keep this test's take if it fails
@pytest.mark.retake(expect_errors=[429]) # record real 429s and their retries
def test_partial(): ...
The retake_take fixture returns what Retake said at test/begin: the take path and, in
replay, recorded_at (useful for freezing time; see below).
How the test-ID header is attached
The plugin wraps Client.send and AsyncClient.send of httpx2 and httpx, whichever
are installed. The official anthropic SDK sends every request through one of them
(httpx2 since anthropic 2.x, httpx before; openai uses httpx), so this works for
clients created at any time (module level, session fixtures, inside the test), for sync
and async clients, retries and streaming. Only requests to the proxy's host and port are
tagged.
The header names the test currently running in the process; pytest runs one test at a
time per process (pytest-xdist workers are processes, each with its own current test).
Limits:
- Fixtures wider than a function (session, module, class scope) are set up and torn
down outside any test: their requests are untagged and go to
_session.take, even though pytest runs them during the setup or teardown of some test. - Requests from threads or asyncio tasks the test starts are attributed to the test while
it runs. Work that outlives the test is sent untagged and lands in
_session.takeinstead of being rejected as a late request. - HTTP clients built on neither (
requests,aiohttp,urllib) are not tagged. Add the header yourself withpytest_retake.test_headers(). - Running several tests concurrently in one process (e.g.
pytest-asyncio-cooperative,pytest-parallelthreads) would mix their attributions; not supported. - Rerunning a test in the same run (
pytest-rerunfailures) is refused by Retake for now.
Not done yet
- Time freezing (ADR 0006 item 8): a helper would freeze the clock at the
recorded_atfromtest/beginduring replay, e.g. withtime-machine, and do nothing while recording. The value is already available from theretake_takefixture.
Development
pytest-retake depends on retake-cli, the wheel that bundles the retake binary, at
the same version. In this repository, uv takes retake-cli from ../retake-cli instead
of PyPI ([tool.uv.sources] in pyproject.toml), so uv sync and
uv run --with ./python/pytest-retake ... build the binary from the working tree with
maturin; a release build takes a minute or two the first time, then uv reuses the wheel
until the commit, Cargo.toml or Cargo.lock changes. MATURIN_PEP517_ARGS="--profile dev" makes it a debug build, as CI does. The plugin's own end-to-end tests do not use
that binary: they run $RETAKE_BIN, else target/debug/retake (cargo build).
Published packages carry no uv sources: pip install pytest-retake gets retake-cli
from PyPI.
These commands test the plugin against your own build of Retake, not an installed
retake:
cargo build # the plugin's end-to-end tests run target/debug/retake
cd python/pytest-retake
uv sync
uv run pytest
uv run ruff check . && uv run ruff format --check .
Licensed under MIT OR Apache-2.0, like Retake.
Metadata
Release files for pytest-retake 0.1.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 | Provenance | Actions |
|---|---|---|---|---|
| pytest_retake-0.1.0.tar.gz | 102.8 kB | With provenance | Details for pytest_retake-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytest_retake-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pytest_retake-0.1.0-py3-none-any.whl |
Total release size: 118.4 kB
Release files / pytest_retake-0.1.0.tar.gz
| Download URL | pytest_retake-0.1.0.tar.gz |
|---|---|
| Size | 102.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
800fb7ee7eabb52605f79d9ed8c907b0b3625e34bcfc307dc0658d773bd05797
|
|
BLAKE2b-256 checksum How to use checksums |
12bbdd3219a21e15c3d39ef9a4b447a096940876da27592e9cd312e346798f2f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Dec 6, 2026.
Transparency logRelease files / pytest_retake-0.1.0-py3-none-any.whl
| Download URL | pytest_retake-0.1.0-py3-none-any.whl |
|---|---|
| Size | 15.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9ac5ef50c6d3058deea27719c63663d05420ab726e86f61c02addf0bdf4d6644
|
|
BLAKE2b-256 checksum How to use checksums |
8b1f262e5c7b0dd8d2c7bd8963fb333af76f7241c50f89791cbd9f7bae5509c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Dec 6, 2026.
Transparency logSecurity for pytest-retake 0.1.0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for pytest_retake-0.1.0.tar.gz
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 Dec 6, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.