snapshottest

repository·master·Indexed 19 days ago

https://github.com/syrusakbary/snapshottest

A 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.

Tokens
923
Snippets
5
Records
5
Agent score
19%

What's inside snapshottest

  1. Use snapshottest with Django

    master

    To integrate snapshottest with Django:

    1. Update your Django settings to use the snapshottest test runner: TEST_RUNNER = 'snapshottest.django.TestRunner'

    2. Inherit from snapshottest.django.TestCase in your test files.

    3. 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-update
  2. Use snapshottest with pytest

    master

    To 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')
  3. Use snapshottest with unittest or nose

    master

    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')