pytest-accept
pytest-accept is a pytest plugin for automatically updating outputs. It runs along with pytest, observes the generated outputs, and writes them to the test's documented outputs.
Before
def extract_functions(code):
return re.findall(r"(\w+)\(", code)
def test_extract_functions():
assert extract_functions("print('hi')") == ["print"]
assert extract_functions("sum(map(f, x))") == ["sum"]
After pytest --accept
def test_extract_functions():
assert extract_functions("print('hi')") == ["print"]
- assert extract_functions("sum(map(f, x))") == ["sum"]
+ assert extract_functions("sum(map(f, x))") == ["sum", "map"]
pytest-accept is decoupled from the tests it works with — it can be used with
existing tests, and the tests it edits are no different from normal tests. It
works with both doctests and normal assert statements.
Installation
uv tool install -U pytest-accept
Or with pip:
pip install pytest-accept
To run, just pass --accept to pytest:
pytest --accept
Why?
- Often it's fairly easy to observe whether something is working by viewing the output it produces
- ...but often the output is verbose, and copying and pasting the output into the test is tedious
pytest-acceptdoes the copying & pasting for you- Similarly, lots of folks generally find writing any tests a bit annoying, and prefer to develop by "running the code and seeing if it works". This library aims to make testing a joyful part of that development loop
This style of testing is fairly well-developed in some languages, although still doesn't receive the attention I think it deserves, and historically hasn't had good support in python.
The best explanation I've seen on this testing style is from @yminsky in a Jane Street Blogpost. @matklad also has an excellent summary in his blog post How to Test.
How it works
pytest-accept:
- Intercepts test failures from both doctests and assert statements
- Parses the files to understand where the documented values are
- Updates the documented values to match the generated values
- Writes everything back atomically
Things to know:
- Simple comparisons only: Assert rewriting only works with
==comparisons against literals or simple expressions - Overwrite by default: Pass
--accept-copyto write to.py.newfiles instead.
Doctest quirks
Doctests are great for examples, but they have quirks
-
Use raw strings for examples with backslashes:
r""" >>> print("\n") \n """
-
We handle blank lines automatically:
""" >>> print("one\n\ntwo") one <BLANKLINE> two """
-
Really long outputs get truncated so they won't break your editor
Prior art
This testing style goes by many names: "snapshot testing", "regression testing", "expect testing", "literate testing", or "acceptance testing". Whatever the name, the pattern is the same: write tests, see what they produce, accept what's correct.
@matklad has an excellent overview in How to Test. The approach is well-established in many languages:
- cram (command-line tests)
- ppx_expect (OCaml)
- insta (Rust)
For more complex test scenarios, consider:
- pytest-regtest for file-based testing
- syrupy for snapshot testing
- pytest-insta for insta-style review
thanks to @untiaker, who found how to expand the
original doctest solution into an approach that works with standard assert
statements.
Release files for pytest-accept 0.3.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 | |
|---|---|---|---|
| pytest_accept-0.3.0.tar.gz | 29.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytest_accept-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 69.6 kB
Release files / pytest_accept-0.3.0.tar.gz
| Download URL | pytest_accept-0.3.0.tar.gz |
|---|---|
| Size | 29.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6f4e03e2492621e10b7678a331828274498cca04686b15b0f3e704e16e651068
|
|
BLAKE2b-256 checksum How to use checksums |
6f42573e6dcd8d1a5fc779d525268d5036f710d018cdeefbae2bd5559687a6d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 11, 2026.
Transparency logRelease files / pytest_accept-0.3.0-py3-none-any.whl
| Download URL | pytest_accept-0.3.0-py3-none-any.whl |
|---|---|
| Size | 39.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5e73f27020853b861a63dc630dfe63124af4bd60e0e0bb692e1376b99798f2b5
|
|
BLAKE2b-256 checksum How to use checksums |
064079f6f13e616f580b9c35110376818c76cacc54b70d2965bf1f82be336c66
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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 Jun 11, 2026.
Transparency log