Skip to main content

compare test results with snapshots from previous test runs

Project description

inline-snapshot

create and update inline snapshots in your code.

Features

  • records current values during pytest run --update-snapshots=new.
  • values are stored in the source code and not in separate files.
  • values can be updated with --update-snapshots=failing.

Installation

You can install "inline-snapshot" via pip from PyPI::

$ pip install inline-snapshot

Usage

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

def something():
    return 1548 * 18489


def test_something():
    assert something() == snapshot()

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

$ pytest --update-snapshots=new
def something():
    return 1548 * 18489


def test_something():
    assert something() == snapshot(28620972)  # snapshot gets recorded

Your tests will break if you change your code later. You get normal pytest failure messages, because snapshot(value) just returns value during normal test runs.

def something():
    return (1548 * 18489) // 18  # changed implementation


def test_something():
    assert something() == snapshot(28620972)  # this will fail now

Maybe that is correct and you should fix your code, or your code is correct and you want to update your test results.

$ pytest --update-snapshots=failing

Please verify the new results. git diff will give you a good overview over all changed results. Use pytest -k test_something --update-snapshots=failing if you only want to change one test.

def something():
    return (1548 * 18489) // 18


def test_something():
    assert something() == snapshot(1590054)

The code is generated without any formatting. Use the formatter of your choice to make it look nice, or maybe use darker if you only want to format your changes.

More than just numbers

Requirements:

  • snapshot(value) can only be used for == comparison
  • the values should be comparable with ==
  • repr(value) should return valid python code

You can use almost any python datatype and also complex values like datatime.date (you have to import the right modules to match the repr() output).

from inline_snapshot import snapshot
import datetime


def something():
    return {
        "name": "hello",
        "one number": 5,
        "numbers": list(range(10)),
        "sets": {1, 2, 15},
        "datetime": datetime.date(1, 2, 22),
        "complex stuff": 5j + 3,
        "bytes": b"fglecg\n\x22",
    }


def test_something():
    assert something() == snapshot(
        {
            "name": "hello",
            "one number": 5,
            "numbers": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            "sets": {1, 2, 15},
            "datetime": datetime.date(1, 2, 22),
            "complex stuff": (3 + 5j),
            "bytes": b'fglecg\n"',
        }
    )

snapshot() can also be used in loops.

from inline_snapshot import snapshot


def test_loop():
    for name in ["Mia", "Ava", "Leo"]:
        assert len(name) == snapshot(3)

… and more to come :grin:.

Contributing

Contributions are very welcome. Tests can be run with tox. Please use pre-commit for your commits.

License

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

Issues

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

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

inline-snapshot-0.1.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

inline_snapshot-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file inline-snapshot-0.1.0.tar.gz.

File metadata

  • Download URL: inline-snapshot-0.1.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.0 Linux/4.19.0-21-amd64

File hashes

Hashes for inline-snapshot-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d38c167f4f5f3a31ddf87ab70026580d62d17cc66d97567d4dfa6cc2749b3d9e
MD5 152b36008d0272e2fd4040ed3bc48997
BLAKE2b-256 cfc50704b0708df7cbccde1034b1e7cefa472096e0f45cd33a462298827bcfb3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: inline_snapshot-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.0 Linux/4.19.0-21-amd64

File hashes

Hashes for inline_snapshot-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3118acb4e2075dccd8e39f87a02d4754526794edc04df5d749ef60c215a60153
MD5 3fb961d11c1d395f0011a30c4b08c4e9
BLAKE2b-256 38b8f8ee0eb025143d64c4963950c55094ca7d879ffa12eb2828b55145cde85d

See more details on using hashes here.

Supported by

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