Skip to main content

PyTest Snapshot Test Utility

Project description

syrupy

Logo

All Contributors Maturity badge - level 2 Stage Slack workspace

Pytest>=5.1.0 Pypi Wheel PyPI - Python Version PyPI - Downloads PyPI - License

Build Status codecov

Overview

Syrupy is a pytest snapshot plugin. It enables developers to write tests which assert immutability of computed results.

Motivation

The most popular snapshot test plugin compatible with pytest has some core limitations which this package attempts to address by upholding some key values:

  • Extensible: If a particular data type is not supported, users should be able to easily and quickly add support.
  • Idiomatic: Snapshot testing should fit naturally among other test cases in pytest, e.g. assert x == snapshot vs. snapshot.assert_match(x).
  • Soundness: Snapshot tests should uncover even the most minute issues. Unlike other snapshot libraries, Syrupy will fail a test suite if a snapshot does not exist, not just on snapshot differences.

Installation

python -m pip install syrupy

Migration from snapshottest

You cannot use syrupy alongside snapshottest due to argument conflicts. To ease migration, we've made syrupy aware of snapshottest call syntax. Simply uninstall snapshottest and remove old snapshots:

pip uninstall snapshottest -y;
find . -type d ! -path '*/\.*' -name 'snapshots' | xargs rm -r

there can only be one

Usage

Basic Usage

In a pytest test file test_file.py:

def test_foo(snapshot):
    actual = "Some computed value!"
    assert actual == snapshot

when you run pytest, the above test should fail due to a missing snapshot. Re-run pytest with the update snapshots flag like so:

pytest --snapshot-update

A snapshot file should be generated under a __snapshots__ directory in the same directory as test_file.py. The __snapshots__ directory and all its children should be committed along with your test code.

Usage Demo

Custom Objects

The default serializer supports all python built-in types and provides a sensible default for custom objects.

If you need to customise your object snapshot, it is as easy as overriding the default __repr__ implementation.

def __repr__(self) -> str:
    return "MyCustomClass(...)"

Options

These are the cli options exposed to pytest by the plugin.

Option Description Default
--snapshot-update Snapshots will be updated to match assertions and unused snapshots will be deleted. False
--snapshot-warn-unused Prints a warning on unused snapshots rather than fail the test suite. False
--snapshot-default-extension Use to change the default snapshot extension class. syrupy.extensions.amber.AmberSnapshotExtension

Built-In Extensions

Syrupy comes with a few built-in preset configurations for you to choose from. You should also feel free to extend the AbstractSyrupyExtension if your project has a need not captured by one our built-ins.

  • AmberSnapshotExtension: This is the default extension which generates .ambr files. Serialization of most data types are supported.
  • SingleFileSnapshotExtension: Unlike the AmberSnapshotExtension, which groups all tests within a single test file into a singular snapshot file, this extension creates one .raw file per test case.
  • PNGSnapshotExtension: An extension of single file, this should be used to produce .png files from a byte string.
  • SVGSnapshotExtension: Another extension of single file. This produces .svg files from an svg string.

Advanced Usage

By overriding the provided AbstractSnapshotExtension you can implement varied custom behaviours.

See examples of how syrupy can be used and extended in the test examples.

Extending Syrupy

Uninstalling

pip uninstall syrupy

If you have decided not to use Syrupy for your project after giving us a try, we'd love to get your feedback. Please create a GitHub issue if applicable, or drop a comment in our Slack channel.

Contributing

Feel free to open a PR or GitHub issue. Contributions welcome!

To develop locally, clone this repository and run . script/bootstrap to install test dependencies. You can then use invoke --list to see available commands.

See contributing guide

Contributors


Noah

🚇 🤔 💻 📖 ⚠️

Emmanuel Ogbizi

💻 🎨 🚇 📖 ⚠️

Adam Lazzarato

📖

Marc Cataford

💻 ⚠️

Michael Rose

💻 ⚠️

Jimmy Jia

💻 ⚠️

This section is automatically generated via tagging the all-contributors bot in a PR:

@all-contributors please add <username> for <contribution type>

License

Syrupy is licensed under Apache License Version 2.0.

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

syrupy-0.3.11.tar.gz (6.3 MB view details)

Uploaded Source

Built Distribution

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

syrupy-0.3.11-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file syrupy-0.3.11.tar.gz.

File metadata

  • Download URL: syrupy-0.3.11.tar.gz
  • Upload date:
  • Size: 6.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for syrupy-0.3.11.tar.gz
Algorithm Hash digest
SHA256 1b00da5fc63a248fd492700a0d30617c217a0a9d6c6c004b3f37ffa565977fc3
MD5 98c1adf5f35023856466beaad83dde94
BLAKE2b-256 bbc1818318295d14906c1f285945831605a19efcb9c1f3d01895c1fc76103a54

See more details on using hashes here.

File details

Details for the file syrupy-0.3.11-py3-none-any.whl.

File metadata

  • Download URL: syrupy-0.3.11-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for syrupy-0.3.11-py3-none-any.whl
Algorithm Hash digest
SHA256 521ccbfb89759b1cb37e99820c52286d5048893b9acffeeb7b4144fda97f809c
MD5 062681f7dbe7b545ccb24ec78bae9776
BLAKE2b-256 b21bad9b4a3b3600d3ffb6933a908381faf9b868d103a01fd72f3684af3a8f9c

See more details on using hashes here.

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