Install snapshottest via pip
masterInstall the snapshottest package using pip to enable snapshot testing for your APIs.
$ pip install snapshottest```bash
$ pip install snapshottest
```埋repository·master·Indexed 19 days ago
https://github.com/syrusakbary/snapshottestA Python library for snapshot testing of APIs that captures API responses as files for automatic comparison in future test runs. It provides integration for unittest, nose, pytest, and Django, allowing developers to detect unexpected changes without writing manual assertions for every field.
Install the snapshottest package using pip to enable snapshot testing for your APIs.
$ pip install snapshottest```bash
$ pip install snapshottest
```埋To integrate snapshottest with Django:
Update your Django settings to use the snapshottest test runner:
TEST_RUNNER = 'snapshottest.django.TestRunner'
Inherit from snapshottest.django.TestCase in your test files.
Use assertMatchSnapshot to validate responses.
To automatically update snapshots when running Django tests, use the --snapshot-update flag with manage.py.
# In settings.py
TEST_RUNNER = 'snapshottest.django.TestRunner'
# In tests.py
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)Command to update snapshots:
python manage.py test --snapshot-updateTo use snapshottest with pytest, use the snapshot fixture in your test functions. Call snapshot.assert_match(response) to perform the comparison. You can provide an optional second argument to specify a custom snapshot name.
To automatically update snapshots when running pytest, use the --snapshot-update flag.
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')To use snapshottest with unittest or nose, inherit from snapshottest.TestCase. Use the assertMatchSnapshot method to compare an API response against a saved snapshot. You can provide an optional second argument to specify a custom snapshot name.
To automatically update snapshots when running tests with nose, use the --snapshot-update flag.
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 need to disable terminal colors (e.g., in CI environments), set the ANSI_COLORS_DISABLED environment variable to any value.
ANSI_COLORS_DISABLED=1 pytest