Giter VIP home page Giter VIP logo

snapshottest's Introduction

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.

Contributing

After cloning this repo, ensure dependencies are installed by running:

pip install -e ".[test]"

After developing, the full test suite can be evaluated by running:

py.test

Notes

This package is heavily insipired in jest snapshot testing.

Reasons for 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

snapshottest's People

Contributors

syrusakbary avatar jdufresne avatar caffodian avatar dmr avatar dfee avatar tricoder42 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.