Skip to main content

Snapshot testing for pytest, unittest, Django, and Nose

Project description

SnapshotTest travis pypi

Snapshot testing is a way to test your APIs without writing actual test cases.

  1. A snapshot is a single state of your API, saved in a file.
  2. You have a set of snapshots for your API endpoints.
  3. Once you add a new feature, you can generate automatically new snapshots for the updated API.

Installation

$ pip install snapshottest

Usage with unittest/nose

from snapshottest import TestCase

class APITestCase(TestCase):
    def test_api_me(self):
        """Testing the API for /me"""
        my_api_response = api.client.get('/me')
        self.assertMatchSnapshot(my_api_response)

        # Set custom snapshot name: `gpg_response`
        my_gpg_response = api.client.get('/me?gpg_key')
        self.assertMatchSnapshot(my_gpg_response, 'gpg_response')

If you want to update the snapshots automatically you can use the nosetests --snapshot-update.

Check the Unittest example.

Usage with pytest

def test_mything(snapshot):
    """Testing the API for /me"""
    my_api_response = api.client.get('/me')
    snapshot.assert_match(my_api_response)

    # Set custom snapshot name: `gpg_response`
    my_gpg_response = api.client.get('/me?gpg_key')
    snapshot.assert_match(my_gpg_response, 'gpg_response')

If you want to update the snapshots automatically you can use the --snapshot-update config.

Check the Pytest example.

Usage with django

Add to your settings:

TEST_RUNNER = 'snapshottest.django.TestRunner'

To create your snapshottest:

from snapshottest.django import TestCase

class APITestCase(TestCase):
    def test_api_me(self):
        """Testing the API for /me"""
        my_api_response = api.client.get('/me')
        self.assertMatchSnapshot(my_api_response)

If you want to update the snapshots automatically you can use the python manage.py test --snapshot-update. Check the Django example.

Disabling terminal colors

Set the environment variable ANSI_COLORS_DISABLED (to any value), e.g.

ANSI_COLORS_DISABLED=1 pytest

Contributing

After cloning this repo and configuring a virtualenv for snapshottest (optional, but highly recommended), ensure dependencies are installed by running:

make develop

After developing, ensure your code is formatted properly by running:

make format-fix

and then run the full test suite with:

make lint
# and
make test

To test locally on all supported Python versions, you can use tox:

pip install tox  # (if you haven't before)
tox

Notes

This package is heavily inspired in jest snapshot testing.

Reasons to use this package

Most of this content is taken from the Jest snapshot blogpost.

We want to make it as frictionless as possible to write good tests that are useful. We observed that when engineers are provided with ready-to-use tools, they end up writing more tests, which in turn results in stable and healthy code bases.

However engineers frequently spend more time writing a test than the component itself. As a result many people stopped writing tests altogether which eventually led to instabilities.

A typical snapshot test case for a mobile app renders a UI component, takes a screenshot, then compares it to a reference image stored alongside the test. The test will fail if the two images do not match: either the change is unexpected, or the screenshot needs to be updated to the new version of the UI component.

Snapshot Testing with SnapshotTest

A similar approach can be taken when it comes to testing your APIs. Instead of rendering the graphical UI, which would require building the entire app, you can use a test renderer to quickly generate a serializable value for your API response.

License

MIT License

coveralls

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

snapshottest-1.0.0a0.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

snapshottest-1.0.0a0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file snapshottest-1.0.0a0.tar.gz.

File metadata

  • Download URL: snapshottest-1.0.0a0.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.5

File hashes

Hashes for snapshottest-1.0.0a0.tar.gz
Algorithm Hash digest
SHA256 dfcde50b4bc8a9336a40bfae1127e2f1310f6bbab4394ffcd0cc192f4c929ba0
MD5 98852f91690b2fc7bb19ead7d4a7f970
BLAKE2b-256 02fcebe5acfecb341254659b0603a735b6f4e9e61d33cd76f8a43aa2c5ef847b

See more details on using hashes here.

Provenance

File details

Details for the file snapshottest-1.0.0a0-py3-none-any.whl.

File metadata

  • Download URL: snapshottest-1.0.0a0-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.5

File hashes

Hashes for snapshottest-1.0.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 d26f561e8c0ef260c5da9aedc62727c9830a978d22199b637269c325d496d645
MD5 cef7d8c8b7571ba8e80031cb25a17a61
BLAKE2b-256 37cc8e65a7a20d448c561587078bf2e183c5d5f4580e860bf91f7fc1e37eb273

See more details on using hashes here.

Provenance

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