Skip to main content

No project description provided

Project description

pytest-accept

GitHub Workflow CI Status PyPI Version GitHub License

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 test_calculate_total():
    assert calculate_total([10, 20, 30], 0.1) == 55
    assert calculate_total([5, 5], 0.2) == 10

After pytest --accept

def test_calculate_total():
-    assert calculate_total([10, 20, 30], 0.1) == 55
-    assert calculate_total([5, 5], 0.2) == 10
+    assert calculate_total([10, 20, 30], 0.1) == 66.0
+    assert calculate_total([5, 5], 0.2) == 12.0

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-accept does 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-copy to write to .py.new files 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:

For more complex test scenarios, consider:

thanks to @untiaker, who found how to expand the original doctest solution into an approach that works with standard assert statements.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_accept-0.2.1.tar.gz (24.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_accept-0.2.1-py3-none-any.whl (33.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest_accept-0.2.1.tar.gz.

File metadata

  • Download URL: pytest_accept-0.2.1.tar.gz
  • Upload date:
  • Size: 24.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytest_accept-0.2.1.tar.gz
Algorithm Hash digest
SHA256 8c143f1bff5adf321c505ef3904fb1bcec88b63bc2dfc97ce33ec1d3af574f2f
MD5 748aab9e009ec177efdb301f3a472f90
BLAKE2b-256 e2fee13fc5f4a01a114c1175e289337e23a76c40eb5562f5a9aafff736cb618d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_accept-0.2.1.tar.gz:

Publisher: pypi-release.yaml on max-sixty/pytest-accept

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_accept-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pytest_accept-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 33.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytest_accept-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 52acea7cf14b4369a5aeabdea699687342648c2a2da9fe453a2e5fe3f60a0f96
MD5 43a1c1c131315c9c41346d181ed27434
BLAKE2b-256 eb526774750e81fb076a6fbbb5ffea15797615eb098e47b56989c6b5ec5a128b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_accept-0.2.1-py3-none-any.whl:

Publisher: pypi-release.yaml on max-sixty/pytest-accept

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page