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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: inline_snapshot-0.1.2.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/5.10.0-19-amd64

File hashes

Hashes for inline_snapshot-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5ecd491edfa9819a7014eb3e5c0833afe3a9bf67f918a5a537501641d9948e50
MD5 9678c552b1ffe7df35fe9a3258bd03e0
BLAKE2b-256 b26e7aa8609cf4e692499e3275ec19e1fa3bb6fdbc1dd0d0c401845f0e6c0557

See more details on using hashes here.

File details

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

File metadata

  • Download URL: inline_snapshot-0.1.2-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/5.10.0-19-amd64

File hashes

Hashes for inline_snapshot-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 af4b03c4b1923245d82459ca48c86555382389ef7029498fd3d2dc50544833ea
MD5 522e3da80da6f28ed6e4db251e44ac07
BLAKE2b-256 b76e2e756c0bd5fa03b4d2bbfc941aed69b579ed34dba94e4985c6acde63a1b5

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