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.1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: inline_snapshot-0.1.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.11.0 Linux/4.19.0-22-amd64

File hashes

Hashes for inline_snapshot-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f28526ce6f07b7e61b00f4b427d66e42b610889cff74d0d946419292bad543d9
MD5 8dcada6b1d0dac12cf2a0b228a37ec4b
BLAKE2b-256 97ac9c82539aa27289182d2f9d8b75d5dc89fe7c61d59203d61b63f670b6a567

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for inline_snapshot-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9cdf4d2de894f499c0df833eee6c0d8ff6d86959258744889959561403103f29
MD5 ba75b18087cdad7d758f4cbea0d49d84
BLAKE2b-256 7a9830ffa6fa1578ffc35be39f1f9e749658347483b0c5f5b7191099a8998e21

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