# SnapshotTest [![travis][travis-image]][travis-url] [![pypi][pypi-image]][pypi-url]
[travis-image]: https://img.shields.io/travis/syrusakbary/snapshottest.svg?style=flat
[travis-url]: https://travis-ci.org/syrusakbary/snapshottest
[pypi-image]: https://img.shields.io/pypi/v/snapshottest.svg?style=flat
[pypi-url]: https://pypi.python.org/pypi/snapshottest
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
```python
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](https://github.com/syrusakbary/snapshottest/tree/master/examples/unittest).
## Usage with pytest
```python
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](https://github.com/syrusakbary/snapshottest/tree/master/examples/pytest).
## Usage with django
Add to your settings:
```python
TEST_RUNNER = 'snapshottest.django.TestRunner'
```
To create your snapshottest:
```python
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](https://github.com/syrusakbary/snapshottest/tree/master/examples/django_project).
## 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:
```sh
make develop
```
After developing, ensure your code is formatted properly by running:
```sh
make format-fix
```
and then run the full test suite with:
```sh
make lint
# and
make test
```
To test locally on all supported Python versions, you can use
[tox](https://tox.readthedocs.io/):
```sh
pip install tox # (if you haven't before)
tox
```
# Notes
This package is heavily inspired in [jest snapshot testing](https://facebook.github.io/jest/docs/snapshot-testing.html).
# Reasons to use this package
> Most of this content is taken from the [Jest snapshot blogpost](https://facebook.github.io/jest/blog/2016/07/27/jest-14.html).
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](https://github.com/syrusakbary/snapshottest/blob/master/LICENSE)
[![coveralls][coveralls-image]][coveralls-url]
[coveralls-image]: https://coveralls.io/repos/syrusakbary/snapshottest/badge.svg?branch=master&service=github
[coveralls-url]: https://coveralls.io/github/syrusakbary/snapshottest?branch=master
Raw data
{
"_id": null,
"home_page": "https://github.com/syrusakbary/snapshottest",
"name": "snapshottest-patch",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Syrus Akbary",
"author_email": "me@syrusakbary.com",
"download_url": "https://files.pythonhosted.org/packages/2f/a1/03bb6235daf211032cf1b63e43ec8f46eb89e002115daf1253a3772e4bda/snapshottest-patch-1.0.0.tar.gz",
"platform": null,
"description": "# SnapshotTest [![travis][travis-image]][travis-url] [![pypi][pypi-image]][pypi-url]\n\n[travis-image]: https://img.shields.io/travis/syrusakbary/snapshottest.svg?style=flat\n[travis-url]: https://travis-ci.org/syrusakbary/snapshottest\n[pypi-image]: https://img.shields.io/pypi/v/snapshottest.svg?style=flat\n[pypi-url]: https://pypi.python.org/pypi/snapshottest\n\n\nSnapshot testing is a way to test your APIs without writing actual test cases.\n\n1. A snapshot is a single state of your API, saved in a file.\n2. You have a set of snapshots for your API endpoints.\n3. Once you add a new feature, you can generate *automatically* new snapshots for the updated API.\n\n## Installation\n\n $ pip install snapshottest\n\n\n## Usage with unittest/nose\n\n```python\nfrom snapshottest import TestCase\n\nclass APITestCase(TestCase):\n def test_api_me(self):\n \"\"\"Testing the API for /me\"\"\"\n my_api_response = api.client.get('/me')\n self.assertMatchSnapshot(my_api_response)\n\n # Set custom snapshot name: `gpg_response`\n my_gpg_response = api.client.get('/me?gpg_key')\n self.assertMatchSnapshot(my_gpg_response, 'gpg_response')\n```\n\nIf you want to update the snapshots automatically you can use the `nosetests --snapshot-update`.\n\nCheck the [Unittest example](https://github.com/syrusakbary/snapshottest/tree/master/examples/unittest).\n\n## Usage with pytest\n\n```python\ndef test_mything(snapshot):\n \"\"\"Testing the API for /me\"\"\"\n my_api_response = api.client.get('/me')\n snapshot.assert_match(my_api_response)\n\n # Set custom snapshot name: `gpg_response`\n my_gpg_response = api.client.get('/me?gpg_key')\n snapshot.assert_match(my_gpg_response, 'gpg_response')\n```\n\nIf you want to update the snapshots automatically you can use the `--snapshot-update` config.\n\nCheck the [Pytest example](https://github.com/syrusakbary/snapshottest/tree/master/examples/pytest).\n\n## Usage with django\nAdd to your settings:\n```python\nTEST_RUNNER = 'snapshottest.django.TestRunner'\n```\nTo create your snapshottest:\n```python\nfrom snapshottest.django import TestCase\n\nclass APITestCase(TestCase):\n def test_api_me(self):\n \"\"\"Testing the API for /me\"\"\"\n my_api_response = api.client.get('/me')\n self.assertMatchSnapshot(my_api_response)\n```\nIf you want to update the snapshots automatically you can use the `python manage.py test --snapshot-update`.\nCheck the [Django example](https://github.com/syrusakbary/snapshottest/tree/master/examples/django_project).\n\n## Disabling terminal colors\n\nSet the environment variable `ANSI_COLORS_DISABLED` (to any value), e.g. \n\n ANSI_COLORS_DISABLED=1 pytest\n\n\n# Contributing\n\nAfter cloning this repo and configuring a virtualenv for snapshottest (optional, but highly recommended), ensure dependencies are installed by running:\n\n```sh\nmake develop\n```\n\nAfter developing, ensure your code is formatted properly by running:\n \n```sh\nmake format-fix\n```\n\nand then run the full test suite with:\n\n```sh\nmake lint\n# and\nmake test\n```\n\nTo test locally on all supported Python versions, you can use \n[tox](https://tox.readthedocs.io/):\n\n```sh\npip install tox # (if you haven't before)\ntox\n```\n\n# Notes\n\nThis package is heavily inspired in [jest snapshot testing](https://facebook.github.io/jest/docs/snapshot-testing.html).\n\n# Reasons to use this package\n\n> Most of this content is taken from the [Jest snapshot blogpost](https://facebook.github.io/jest/blog/2016/07/27/jest-14.html).\n\nWe want to make it as frictionless as possible to write good tests that are useful.\nWe 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.\n\nHowever 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.\n\nA 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.\n\n\n## Snapshot Testing with SnapshotTest\n\nA similar approach can be taken when it comes to testing your APIs.\nInstead 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.\n\n\n## License\n\n[MIT License](https://github.com/syrusakbary/snapshottest/blob/master/LICENSE)\n\n[![coveralls][coveralls-image]][coveralls-url]\n\n[coveralls-image]: https://coveralls.io/repos/syrusakbary/snapshottest/badge.svg?branch=master&service=github\n[coveralls-url]: https://coveralls.io/github/syrusakbary/snapshottest?branch=master\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Snapshot testing for pytest, unittest, Django, and Nose",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://github.com/syrusakbary/snapshottest"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9ec2f92ca2b1450f7dee9fa736a41f4fff70726bf5351f22bbc7cfc9a80c4076",
"md5": "8fbb8f99c0da13b651df57f5079aa062",
"sha256": "1d6811904aa940c10acabc6462c3ee86dc3a9386177e4f8eefea354fb33212c0"
},
"downloads": -1,
"filename": "snapshottest_patch-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8fbb8f99c0da13b651df57f5079aa062",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 16678,
"upload_time": "2023-10-14T00:55:24",
"upload_time_iso_8601": "2023-10-14T00:55:24.207419Z",
"url": "https://files.pythonhosted.org/packages/9e/c2/f92ca2b1450f7dee9fa736a41f4fff70726bf5351f22bbc7cfc9a80c4076/snapshottest_patch-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2fa103bb6235daf211032cf1b63e43ec8f46eb89e002115daf1253a3772e4bda",
"md5": "19becf55293c86e2ae2c6aaba3804035",
"sha256": "773ac39be1d4d5fbb52fd7980a47ca29e6aaa3da107e4632816565476f6947c5"
},
"downloads": -1,
"filename": "snapshottest-patch-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "19becf55293c86e2ae2c6aaba3804035",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 24691,
"upload_time": "2023-10-14T00:55:26",
"upload_time_iso_8601": "2023-10-14T00:55:26.422759Z",
"url": "https://files.pythonhosted.org/packages/2f/a1/03bb6235daf211032cf1b63e43ec8f46eb89e002115daf1253a3772e4bda/snapshottest-patch-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-14 00:55:26",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "syrusakbary",
"github_project": "snapshottest",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"tox": true,
"lcname": "snapshottest-patch"
}