Skip to main content

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].

  • No CLI arguments required: you will get a nice report where you can review the snapshot changes when you run pytest (only for CPython > 3.11, you have to use --inline-snapshot=review on older versions).

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

  • Preserved Formatting: Retains formatting consistency with Black formatting or a custom format-command.

  • External File Storage: Store snapshots externally using external("uuid:eb1167b3-67a9-4378-bc65-c1e582e2e662.json") with support for custom file formats.

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

  • Integration into normal functions: use snapshot_arg() to convert the arguments of your function into snapshots.

  • Customizable: code generation can be customized with @customize

  • 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()
        assert number >= snapshot()

    for c in "hello world":
        # test if something is part of a set
        assert c in snapshot()

    s = snapshot()

    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" * 5) == snapshot()

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

pytest output for pytest

snapshot_arg() can also be used for function parameters:

import subprocess as sp
import sys
from inline_snapshot import snapshot_arg


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


def test_cmd():
    run_python("print('hello world')")

    run_python("1/0")

pytest output for pytest

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 𝕏, 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.

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.35.3.tar.gz (2.5 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.35.3-py3-none-any.whl (95.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: inline_snapshot-0.35.3.tar.gz
  • Upload date:
  • Size: 2.5 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.35.3.tar.gz
Algorithm Hash digest
SHA256 5d76f3b4b134fb190b7883669eb496f5c3e7d1c9549bbd6d78b5b8ac17e553e3
MD5 afff75ce5598fe6bacb7ae3fae919293
BLAKE2b-256 038ebaacae89b98ead1940164468fca01812c66186b12c89523f3ea66efcf87c

See more details on using hashes here.

Provenance

The following attestation bundles were made for inline_snapshot-0.35.3.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.35.3-py3-none-any.whl.

File metadata

File hashes

Hashes for inline_snapshot-0.35.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3decb255b15d6f5956fbc0648c56acf6ceeb9a331b26868e3bf16d2a393c2e31
MD5 12c9ae83e929e7856c7f2e9e776cdc3d
BLAKE2b-256 d69cb8a9697e4c4c98fb25fbb49d70734432cab917c98fa50b258aea687584e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for inline_snapshot-0.35.3-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.

Release history Release notifications | RSS feed

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