No project description provided
Project description
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
import re
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 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_accept-0.2.2.tar.gz.
File metadata
- Download URL: pytest_accept-0.2.2.tar.gz
- Upload date:
- Size: 25.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd8b8f4a43e0007913343c658f2666e176ef6502c39fdaea9177e9f82b6e5fff
|
|
| MD5 |
a3e63d8e342e4ee252233ca06c723337
|
|
| BLAKE2b-256 |
aa33747e2a41725d56f2f5b5740911db24c80eee6cf0a37cdd696396b13eb072
|
Provenance
The following attestation bundles were made for pytest_accept-0.2.2.tar.gz:
Publisher:
pypi-release.yaml on max-sixty/pytest-accept
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_accept-0.2.2.tar.gz -
Subject digest:
dd8b8f4a43e0007913343c658f2666e176ef6502c39fdaea9177e9f82b6e5fff - Sigstore transparency entry: 411158225
- Sigstore integration time:
-
Permalink:
max-sixty/pytest-accept@d68fb2ee1d409251fff5dbcffde6f956dc7a5fd7 -
Branch / Tag:
refs/tags/0.2.2 - Owner: https://github.com/max-sixty
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-release.yaml@d68fb2ee1d409251fff5dbcffde6f956dc7a5fd7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytest_accept-0.2.2-py3-none-any.whl.
File metadata
- Download URL: pytest_accept-0.2.2-py3-none-any.whl
- Upload date:
- Size: 34.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf2b6efd14bfefbfb27d842a94a6037f62fb5257ecb1096cf080b19e388584c1
|
|
| MD5 |
ceb918c124222c1aec3df98c87b11e00
|
|
| BLAKE2b-256 |
2db5c6d16467738532a1463f8747687a0066b1fec7eeb45e49ca5ed312561b19
|
Provenance
The following attestation bundles were made for pytest_accept-0.2.2-py3-none-any.whl:
Publisher:
pypi-release.yaml on max-sixty/pytest-accept
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_accept-0.2.2-py3-none-any.whl -
Subject digest:
cf2b6efd14bfefbfb27d842a94a6037f62fb5257ecb1096cf080b19e388584c1 - Sigstore transparency entry: 411158239
- Sigstore integration time:
-
Permalink:
max-sixty/pytest-accept@d68fb2ee1d409251fff5dbcffde6f956dc7a5fd7 -
Branch / Tag:
refs/tags/0.2.2 - Owner: https://github.com/max-sixty
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-release.yaml@d68fb2ee1d409251fff5dbcffde6f956dc7a5fd7 -
Trigger Event:
release
-
Statement type: