Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Snapshot testing is a way to test your APIs without writing actual test cases.
$ pip install snapshottest
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.
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.
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.
Set the environment variable ANSI_COLORS_DISABLED
(to any value), e.g.
ANSI_COLORS_DISABLED=1 pytest
After cloning this repo and configuring a virtualenv for snapshottest (optional, but highly recommended), ensure dependencies are installed by running:
make install
After developing, the full test suite can be evaluated by running:
make lint
# and
make test
This package is heavily inspired in jest snapshot testing.
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.
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.
FAQs
Snapshot testing for pytest, unittest, Django, and Nose
We found that snapshottest demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.