Snapshot Testing utils for Python
Project description
PySnap
Note: This project is just a fork of the package snapshottest which lives here: https://github.com/syrusakbary/snapshottest. It had been mostly abandoned, so I kicked the wheels and got it back in working order. Contributions are welcome!
Snapshot testing is a way to test your APIs without writing actual test cases.pip install doc8
A snapshot is a single state of your API, saved in a file.
You have a set of snapshots for your API endpoints.
Once you add a new feature, you can generate automatically new snapshots for the updated API.
<p align="center">
</p>
Installation
$ pip install pysnap
Usage with unittest/nose
from pysnap 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 = 'pysnap.django.TestRunner'
To create your pysnap:
from pysnap.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 PySnap
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
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
Built Distribution
File details
Details for the file pysnap-1.0.1.tar.gz
.
File metadata
- Download URL: pysnap-1.0.1.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38be1729221a06a9615afffdd826b27c444a562317a6fb8dd45c7699d841d6f5 |
|
MD5 | ff62b3dc85c63517e9349fabe4bd7872 |
|
BLAKE2b-256 | 422e5b151baebb1e21fb39fffc72862f307d8cfe37906d4752538f371b281e58 |
Provenance
File details
Details for the file pysnap-1.0.1-py2.py3-none-any.whl
.
File metadata
- Download URL: pysnap-1.0.1-py2.py3-none-any.whl
- Upload date:
- Size: 27.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2a77b60c980aa898b1e3db51ede199520606fb017e6a0d901faaa3ffbefb752 |
|
MD5 | f0923a8d8ab3e035b7e94e8aea2b1313 |
|
BLAKE2b-256 | fd86e4fc59abad1d1d8f108b2f91529e6436c0fa96e29bba6e7d5d0710241afb |