Skip to main content

golden master/snapshot/approval testing library which puts the values right into your source code

Project description

inline-snapshot

ci Docs pypi version Python Versions PyPI - Downloads coverage GitHub Sponsors Discord

Installation

You can install "inline-snapshot" via pip:

pip install inline-snapshot

Key Features

  • support for normal assertions: inline-snapshot can now also fix normal assertions which do not use snapshot() like:

    assert 1 + 1 == 3
    

    You can learn here more about this feature.

  • Intuitive Semantics: snapshot(x) mirrors x for easy understanding.

  • Versatile Comparison Support: Equipped with x == snapshot(...), x <= snapshot(...), x in snapshot(...), and snapshot(...)[key].

  • Enhanced Control Flags: Utilize various flags for precise control of which snapshots you want to change.

  • Preserved Black Formatting: Retains formatting consistency with Black formatting.

  • External File Storage: Store snapshots externally using outsource(data).

  • Seamless Pytest Integration: Integrated seamlessly with pytest for effortless testing.

  • Customizable: code generation can be customized with @customize_repr

  • Nested Snapshot Support: snapshots can contain other snapshots

  • Fuzzy Matching: Incorporate dirty-equals for flexible comparisons within snapshots.

  • Dynamic Snapshot Content: snapshots can contain non-constant values

  • Comprehensive Documentation: Access detailed documentation for complete guidance.

Usage

You can use snapshot() instead of the value which you want to compare with.

from inline_snapshot import snapshot


def test_something():
    assert 1548 * 18489 == snapshot()

You can now run the tests and record the correct values.

$ pytest --inline-snapshot=review
from inline_snapshot import snapshot


def test_something():
    assert 1548 * 18489 == snapshot(28620972)

The following examples show how you can use inline-snapshot in your tests. Take a look at the documentation if you want to know more.

from inline_snapshot import external, outsource, snapshot


def test_something():
    for number in range(5):
        # testing for numeric limits
        assert number <= snapshot(4)
        assert number >= snapshot(0)

    for c in "hello world":
        # test if something is part of a set
        assert c in snapshot(["h", "e", "l", "o", " ", "w", "r", "d"])

    s = snapshot(
        {
            0: {"square": 0, "pow_of_two": False},
            1: {"square": 1, "pow_of_two": True},
            2: {"square": 4, "pow_of_two": True},
            3: {"square": 9, "pow_of_two": False},
            4: {"square": 16, "pow_of_two": True},
        }
    )

    for number in range(5):
        # create sub-snapshots at runtime
        assert s[number]["square"] == number**2
        assert s[number]["pow_of_two"] == (
            (number & (number - 1) == 0) and number != 0
        )

    assert outsource("large string\n" * 1000) == snapshot(
        external("hash:8bf10bdf2c30*.txt")
    )

    assert "generates\nmultiline\nstrings" == snapshot(
        """\
generates
multiline
strings\
"""
    )

snapshot() can also be used as parameter for functions:

import subprocess as sp
import sys
from inline_snapshot import snapshot


def run_python(cmd, stdout=None, stderr=None):
    result = sp.run([sys.executable, "-c", cmd], capture_output=True)
    if stdout is not None:
        assert result.stdout.decode() == stdout
    if stderr is not None:
        assert result.stderr.decode() == stderr


def test_cmd():
    run_python(
        "print('hello world')",
        stdout=snapshot(
            """\
hello world
"""
        ),
        stderr=snapshot(""),
    )

    run_python(
        "1/0",
        stdout=snapshot(""),
        stderr=snapshot(
            """\
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ZeroDivisionError: division by zero
"""
        ),
    )

Feedback

inline-snapshot provides some advanced ways to work with snapshots.

I would like to know how these features are used to further improve this small library. Let me know if you've found interesting use cases for this library via twitter, fosstodon or in the github discussions.

Sponsors

I would like to thank my sponsors. Without them, I would not be able to invest so much time in my projects.

Silver sponsor 🥈

logfire

I have also started to offer insider features for inline-snapshot. I will only release features as insider features if they will not cause problems for you when used in an open source project. I hope sponsoring will allow me to spend more time working on open source projects. Thank you for using inline-snapshot, the future will be 🚀.

Issues

If you encounter any problems, please report an issue along with a detailed description.

License

Distributed under the terms of the MIT license, "inline-snapshot" is free and open source software.

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

inline_snapshot-0.32.0.tar.gz (2.6 MB view details)

Uploaded Source

Built Distribution

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

inline_snapshot-0.32.0-py3-none-any.whl (84.1 kB view details)

Uploaded Python 3

File details

Details for the file inline_snapshot-0.32.0.tar.gz.

File metadata

  • Download URL: inline_snapshot-0.32.0.tar.gz
  • Upload date:
  • Size: 2.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for inline_snapshot-0.32.0.tar.gz
Algorithm Hash digest
SHA256 57fa3df325284d0d14def5dab9ac5da89e383f085bea9a7be51fdeab65e59ced
MD5 bcf3127a326b40a4e3e8eaac11eb81c5
BLAKE2b-256 417419294067d5b5b78144eab2aacec85b998c2d2ea6b3d24eefd9a90255d7aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for inline_snapshot-0.32.0.tar.gz:

Publisher: ci.yml on 15r10nk/inline-snapshot

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

File details

Details for the file inline_snapshot-0.32.0-py3-none-any.whl.

File metadata

File hashes

Hashes for inline_snapshot-0.32.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b522ae2c891f666e80213c5f9677ec6fd4a2a7d334ab9d6ce745675bec6a40f0
MD5 c9a57b9ef3d269ebdebabeaee43ae6b4
BLAKE2b-256 97250e84a6322e5fdb1bf67870b2269151449f4894987b26c78718918dd64ea6

See more details on using hashes here.

Provenance

The following attestation bundles were made for inline_snapshot-0.32.0-py3-none-any.whl:

Publisher: ci.yml on 15r10nk/inline-snapshot

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