• Stars
    star
    525
  • Rank 83,812 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Snapshot Testing utils for Python ๐Ÿ“ธ

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

More Repositories

1

gdom

DOM Traversing and Scraping using GraphQL
Python
1,240
star
2

pyjade

Jade template system for Django, Jinja2 and Mako
Python
696
star
3

Flask-SuperAdmin

The best admin interface framework for Flask. With scaffolding for MongoEngine, Django and SQLAlchemy.
Python
635
star
4

validate_email

Validate_email verify if an email address is valid and really exists
Python
544
star
5

react-native-refresher

A pull to refresh ListView for React Native completely written in js.
JavaScript
453
star
6

promise

Ultra-performant Promise implementation in Python
Python
352
star
7

aiodataloader

Asyncio DataLoader for Python3
Python
270
star
8

interpy

Ruby like string interpolation #{} in Python
Python
254
star
9

fastdiff

A fast native implementation of diff algorithm with a pure Python fallback
Python
37
star
10

jsjinja

Jinja2 to Javascript template converter. Now you can render your Jinja2 templates on the client side! ;)
Python
33
star
11

Djinja

(Deprecated) A package that makes possible the integration of Jinja2 in Django, in a clean way.
Python
20
star
12

Webclient-Kohana

Kohana Utilities
PHP
8
star
13

syrusakbary.com

My personal webpage
CSS
6
star
14

django-processorfield

A powerful filefield for Django with multiple processor outputs
Python
5
star
15

PlagiarismDetector-PHP

A Plagiarism Detector system made in PHP
PHP
4
star
16

graphqlprimer

Graphene Twitter Streams Demo for the presentation at Primer.ai
Python
3
star
17

eXercita

Python
3
star
18

flask-beaker

Beaker session interface for Flask.
Python
3
star
19

wasmer-vs-docker

Wasmer vs Docker
Makefile
3
star
20

netlify-wasmer

Netlify wasmer example
2
star
21

Animotion-Java

An animation framework for java. Supports transitions and much more!
2
star
22

assetsy

A vitaminated way to minify, combine and optimize your web resources
Python
1
star
23

gulp-jade-mixins

Jade mixins js-generator plugin for Gulp
JavaScript
1
star
24

PlagiarismDetector_Block-Moodle

The PlagiarismDetector Block for Moodle
1
star
25

django-vagrant-base

Automatically deploy Django site locally with Vagrant
Ruby
1
star
26

RobotExploration-Java

A Robot Interface
Java
1
star
27

wasi-tests

Shell
1
star
28

PlagiarismDetector_Mod-Moodle

The PlagiarismDetector platform for Moodle
PHP
1
star
29

FuzzyUppaal-Java

An implementation of the Uppaal modeling system for the support of fuzzy logic.
Java
1
star
30

tuentify

Te avisa al instante de tus notificaciones pendientes en Tuenti y agiliza la carga de la pรกgina.
JavaScript
1
star
31

wasmer_rust_playground

Playground for the Wasmer Rust Integration
Rust
1
star