Pytest plugin for automatic JSON Schema generation and validation from examples
Project description
🔍 Pytest JsonSchema SnapShot (JSSS)
Plugin for pytest that automatically / manually generates JSON Schemas tests with validates data.
⭐ Star us on GitHub | 📚 Read the Docs | 🐛 Report Bug
✨ Features
- Automatic JSON Schema generation from data examples (using the
gensonlibrary). - Format detection: Automatic detection and validation of string formats (email, UUID, date, date-time, URI, IPv4).
- Schema storage and management.
- Validation of data against saved schemas.
- Schema update via
--schema-update(create new schemas, remove unused ones, update existing). - Support for both
asyncand synchronous functions. - Support for
Uniontypes and optional fields. - Built-in diff comparison of changes via jsonschema-diff.
🚀 Quick Start
Installation
pip install pytest-jsonschema-snapshot
Usage
- Use the
schemashotfixture in your tests
from you_lib import API
from typed_schema_shot import SchemaShot
@pytest.mark.asyncio
async def test_something(schemashot: SchemaShot):
data = await API.get_data()
# There are data - need to validate through the schema
schemashot.assert_json_match(
data, # data for validation / convert to schema
"test_name" # name of the schema
)
schema = await API.get_schema()
# There is a schema (data is optional) - validate by what is
schemashot.assert_schema_match(
schema,
(API.get_schema, "test_name", 1) # == `API.get_schema.test_name.1` filename
data=data # data for validation (optional)
)
-
On first run, generate schemas with the
--schema-updateor--schema-reset(what is the difference? see the documentation) flagpytest --schema-update --save-original
--save-original: save the original data on which the validation was performed. Saving occurs when
--schema-updateor--schema-reset, if you run the schema update without this attribute, the old original data will be deleted without saving new ones. -
On subsequent runs, tests will validate data against saved schemas
pytest
👀 Key Capabilities
-
Union Types: support multiple possible types for fields
-
Optional Fields: automatic detection of required and optional fields
-
Format Detection: automatic detection of string formats including:
Format Example JSON Schema Email user@example.com{"format": "email"}UUID 550e8400-e29b-41d4-a716-446655440000{"format": "uuid"}Date 2023-01-15{"format": "date"}Date-Time 2023-01-01T12:00:00Z{"format": "date-time"}URI https://example.com{"format": "uri"}IPv4 192.168.1.1{"format": "ipv4"} -
Cleanup: automatic removal of unused schemas when running in update mode
-
Schema Summary: colored terminal output showing created, updated, deleted and unused schemas
Advanced Usage? Check the docs!
Best Practices
- Commit schemas to version control: Schemas should be part of your repository
- Review schema changes: When schemas change, review the diffs carefully without
--schema-updateresets. - Clean up regularly: Use
--schema-updateperiodically to remove unused schemas - Descriptive names: Use clear, descriptive names for your schemas
🤝 Contributing
We welcome contributions!
Quick Contribution Setup
# Fork the repo, then:
git clone https://github.com/Miskler/pytest-jsonschema-snapshot.git
cd jsonschema-diff
# Install
make reinstall
# Ensure everything works
make test
make lint
make type-check
# After code editing
make format
📄 License
MIT License - see LICENSE file for details.
Made with ❤️ for developers working with evolving JSON schemas
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_jsonschema_snapshot-0.2.6.tar.gz.
File metadata
- Download URL: pytest_jsonschema_snapshot-0.2.6.tar.gz
- Upload date:
- Size: 200.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
907c269f43e546e26db8b177f6a4149eba3817de2c9dcdbde8bab095f0e42c58
|
|
| MD5 |
9bb094ce6fdb37f1591b4ef580962214
|
|
| BLAKE2b-256 |
5675b892989e036cf695e249e089b9811a93987cd7806c9894b17c05d4b605fe
|
Provenance
The following attestation bundles were made for pytest_jsonschema_snapshot-0.2.6.tar.gz:
Publisher:
release.yml on Miskler/pytest-jsonschema-snapshot
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_jsonschema_snapshot-0.2.6.tar.gz -
Subject digest:
907c269f43e546e26db8b177f6a4149eba3817de2c9dcdbde8bab095f0e42c58 - Sigstore transparency entry: 912857551
- Sigstore integration time:
-
Permalink:
Miskler/pytest-jsonschema-snapshot@cb2f334ca47706a16d95dda678df0c072ba3cb94 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Miskler
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@cb2f334ca47706a16d95dda678df0c072ba3cb94 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file pytest_jsonschema_snapshot-0.2.6-py3-none-any.whl.
File metadata
- Download URL: pytest_jsonschema_snapshot-0.2.6-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6510f08676ecb5663ea626557f488425ad89f1328a214ef55b62531f70bdaa54
|
|
| MD5 |
d32a8b9e9186bff7d5ba1e189439c114
|
|
| BLAKE2b-256 |
dd6b29fb4484ba3fc5775fda1e7201690dfcb5a43326a2bc76cac1c84fb2ed50
|
Provenance
The following attestation bundles were made for pytest_jsonschema_snapshot-0.2.6-py3-none-any.whl:
Publisher:
release.yml on Miskler/pytest-jsonschema-snapshot
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_jsonschema_snapshot-0.2.6-py3-none-any.whl -
Subject digest:
6510f08676ecb5663ea626557f488425ad89f1328a214ef55b62531f70bdaa54 - Sigstore transparency entry: 912857577
- Sigstore integration time:
-
Permalink:
Miskler/pytest-jsonschema-snapshot@cb2f334ca47706a16d95dda678df0c072ba3cb94 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Miskler
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@cb2f334ca47706a16d95dda678df0c072ba3cb94 -
Trigger Event:
workflow_dispatch
-
Statement type: