pytest-jsonschema-snapshot


Namepytest-jsonschema-snapshot JSON
Version 0.2.2 PyPI version JSON
download
home_pageNone
SummaryPytest plugin for automatic JSON Schema generation and validation from examples
upload_time2025-09-02 00:38:37
maintainerNone
docs_urlNone
authorMiskler
requires_python>=3.10
licenseMIT
keywords genson json pytest rich schema validation
VCS
bugtrack_url
requirements genson pathvalidate jsonschema jsonschema-diff pytest types-jsonschema pytest-asyncio pytest-cov pytest-xdist black isort mypy flake8 sphinx sphinx-autoapi furo docutils types-docutils
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
<div align="center">

# 🔍 Pytest JsonSchema SnapShot (JSSS)

<img src="https://raw.githubusercontent.com/Miskler/pytest-jsonschema-snapshot/refs/heads/main/assets/logo.png" width="70%" alt="logo.png" />

***Plugin for pytest that automatically / manually generates JSON Schemas tests with validates data.***

[![Tests](https://miskler.github.io/pytest-jsonschema-snapshot/tests-badge.svg)](https://miskler.github.io/pytest-jsonschema-snapshot/tests/tests-report.html)
[![Coverage](https://miskler.github.io/pytest-jsonschema-snapshot/coverage.svg)](https://miskler.github.io/pytest-jsonschema-snapshot/coverage/)
[![Python](https://img.shields.io/badge/python-3.10+-blue)](https://python.org)
[![PyPI - Package Version](https://img.shields.io/pypi/v/pytest-jsonschema-snapshot?color=blue)](https://pypi.org/project/pytest-jsonschema-snapshot/)
[![License](https://img.shields.io/badge/license-MIT-blue)](LICENSE)
[![BlackCode](https://img.shields.io/badge/code%20style-black-black)](https://github.com/psf/black)
[![mypy](https://img.shields.io/badge/type--checked-mypy-blue?logo=python)](https://mypy.readthedocs.io/en/stable/index.html)
[![Discord](https://img.shields.io/discord/792572437292253224?label=Discord&labelColor=%232c2f33&color=%237289da)](https://discord.gg/UnJnGHNbBp)
[![Telegram](https://img.shields.io/badge/Telegram-24A1DE)](https://t.me/miskler_dev)


**[⭐ Star us on GitHub](https://github.com/Miskler/pytest-jsonschema-snapshot)** | **[📚 Read the Docs](https://miskler.github.io/pytest-jsonschema-snapshot/basic/quick_start.html)** | **[🐛 Report Bug](https://github.com/Miskler/pytest-jsonschema-snapshot/issues)**

## ✨ Features

</div>

![image](https://github.com/user-attachments/assets/2faa2548-5af2-4dc9-8d8d-b32db1d87be8)

* Automatic JSON Schema generation from data examples (using the `genson` library).
* **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 `async` and synchronous functions.
* Support for `Union` types and optional fields.
* Built-in diff comparison of changes via [jsonschema-diff](https://github.com/Miskler/jsonschema-diff).

<div align="center">

## 🚀 Quick Start

</div>

### Installation

```bash
pip install pytest-jsonschema-snapshot
```

### Usage

1. Use the `schemashot` fixture in your tests
  ```python
  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)
      )
  ```

2. On first run, generate schemas with the `--schema-update` or `--schema-reset` (what is the difference? see the documentation) flag
   ```bash
   pytest --schema-update --save-original
   ```

   **--save-original**: save the original data on which the validation was performed. Saving occurs when `--schema-update` or `--schema-reset`, if you run the schema update without this attribute, the old original data will be deleted without saving new ones.

3. On subsequent runs, tests will validate data against saved schemas
   ```bash
   pytest
   ```

<div align="center">

## 👀 Key Capabilities

</div>

* **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](https://miskler.github.io/pytest-jsonschema-snapshot/basic/quick_start.html#then-you-need-to-configure-the-library)!

### Best Practices

1. **Commit schemas to version control**: Schemas should be part of your repository
2. **Review schema changes**: When schemas change, review the diffs carefully without `--schema-update` resets.
3. **Clean up regularly**: Use `--schema-update` periodically to remove unused schemas
4. **Descriptive names**: Use clear, descriptive names for your schemas


<div align="center">

## 🤝 Contributing

### ***We welcome contributions!***

### Quick Contribution Setup

</div>

```bash
# 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
```

<div align="center">

## 📄 License

MIT License - see [LICENSE](LICENSE) file for details.

*Made with ❤️ for developers working with evolving JSON schemas*

</div>

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pytest-jsonschema-snapshot",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "genson, json, pytest, rich, schema, validation",
    "author": "Miskler",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/36/46/7d0b17ab82f46b6da15458c2585991311d731b70bf1b60e5a97fd70c1cbd/pytest_jsonschema_snapshot-0.2.2.tar.gz",
    "platform": null,
    "description": "\n<div align=\"center\">\n\n# \ud83d\udd0d Pytest JsonSchema SnapShot (JSSS)\n\n<img src=\"https://raw.githubusercontent.com/Miskler/pytest-jsonschema-snapshot/refs/heads/main/assets/logo.png\" width=\"70%\" alt=\"logo.png\" />\n\n***Plugin for pytest that automatically / manually generates JSON Schemas tests with validates data.***\n\n[![Tests](https://miskler.github.io/pytest-jsonschema-snapshot/tests-badge.svg)](https://miskler.github.io/pytest-jsonschema-snapshot/tests/tests-report.html)\n[![Coverage](https://miskler.github.io/pytest-jsonschema-snapshot/coverage.svg)](https://miskler.github.io/pytest-jsonschema-snapshot/coverage/)\n[![Python](https://img.shields.io/badge/python-3.10+-blue)](https://python.org)\n[![PyPI - Package Version](https://img.shields.io/pypi/v/pytest-jsonschema-snapshot?color=blue)](https://pypi.org/project/pytest-jsonschema-snapshot/)\n[![License](https://img.shields.io/badge/license-MIT-blue)](LICENSE)\n[![BlackCode](https://img.shields.io/badge/code%20style-black-black)](https://github.com/psf/black)\n[![mypy](https://img.shields.io/badge/type--checked-mypy-blue?logo=python)](https://mypy.readthedocs.io/en/stable/index.html)\n[![Discord](https://img.shields.io/discord/792572437292253224?label=Discord&labelColor=%232c2f33&color=%237289da)](https://discord.gg/UnJnGHNbBp)\n[![Telegram](https://img.shields.io/badge/Telegram-24A1DE)](https://t.me/miskler_dev)\n\n\n**[\u2b50 Star us on GitHub](https://github.com/Miskler/pytest-jsonschema-snapshot)** | **[\ud83d\udcda Read the Docs](https://miskler.github.io/pytest-jsonschema-snapshot/basic/quick_start.html)** | **[\ud83d\udc1b Report Bug](https://github.com/Miskler/pytest-jsonschema-snapshot/issues)**\n\n## \u2728 Features\n\n</div>\n\n![image](https://github.com/user-attachments/assets/2faa2548-5af2-4dc9-8d8d-b32db1d87be8)\n\n* Automatic JSON Schema generation from data examples (using the `genson` library).\n* **Format detection**: Automatic detection and validation of string formats (email, UUID, date, date-time, URI, IPv4).\n* Schema storage and management.\n* Validation of data against saved schemas.\n* Schema update via `--schema-update` (create new schemas, remove unused ones, update existing).\n* Support for both `async` and synchronous functions.\n* Support for `Union` types and optional fields.\n* Built-in diff comparison of changes via [jsonschema-diff](https://github.com/Miskler/jsonschema-diff).\n\n<div align=\"center\">\n\n## \ud83d\ude80 Quick Start\n\n</div>\n\n### Installation\n\n```bash\npip install pytest-jsonschema-snapshot\n```\n\n### Usage\n\n1. Use the `schemashot` fixture in your tests\n  ```python\n  from you_lib import API\n  from typed_schema_shot import SchemaShot\n\n  @pytest.mark.asyncio\n  async def test_something(schemashot: SchemaShot):\n      data = await API.get_data()\n      # There are data - need to validate through the schema\n      schemashot.assert_json_match(\n          data, # data for validation / convert to schema\n          \"test_name\"       # name of the schema\n      )\n\n      schema = await API.get_schema()\n      # There is a schema (data is optional) - validate by what is\n      schemashot.assert_schema_match(\n          schema,\n          (API.get_schema, \"test_name\", 1) # == `API.get_schema.test_name.1` filename\n          data=data # data for validation (optional)\n      )\n  ```\n\n2. On first run, generate schemas with the `--schema-update` or `--schema-reset` (what is the difference? see the documentation) flag\n   ```bash\n   pytest --schema-update --save-original\n   ```\n\n   **--save-original**: save the original data on which the validation was performed. Saving occurs when `--schema-update` or `--schema-reset`, if you run the schema update without this attribute, the old original data will be deleted without saving new ones.\n\n3. On subsequent runs, tests will validate data against saved schemas\n   ```bash\n   pytest\n   ```\n\n<div align=\"center\">\n\n## \ud83d\udc40 Key Capabilities\n\n</div>\n\n* **Union Types**: support multiple possible types for fields\n* **Optional Fields**: automatic detection of required and optional fields\n* **Format Detection**: automatic detection of string formats including:\n\n  | Format | Example | JSON Schema |\n  | --- | --- | --- |\n  | Email | `user@example.com` | `{\"format\": \"email\"}` |\n  | UUID | `550e8400-e29b-41d4-a716-446655440000` | `{\"format\": \"uuid\"}` |\n  | Date | `2023-01-15` | `{\"format\": \"date\"}` |\n  | Date-Time | `2023-01-01T12:00:00Z` | `{\"format\": \"date-time\"}` |\n  | URI | `https://example.com` | `{\"format\": \"uri\"}` |\n  | IPv4 | `192.168.1.1` | `{\"format\": \"ipv4\"}` |\n* **Cleanup**: automatic removal of unused schemas when running in update mode\n* **Schema Summary**: colored terminal output showing created, updated, deleted and unused schemas\n\n## Advanced Usage? Check the [docs](https://miskler.github.io/pytest-jsonschema-snapshot/basic/quick_start.html#then-you-need-to-configure-the-library)!\n\n### Best Practices\n\n1. **Commit schemas to version control**: Schemas should be part of your repository\n2. **Review schema changes**: When schemas change, review the diffs carefully without `--schema-update` resets.\n3. **Clean up regularly**: Use `--schema-update` periodically to remove unused schemas\n4. **Descriptive names**: Use clear, descriptive names for your schemas\n\n\n<div align=\"center\">\n\n## \ud83e\udd1d Contributing\n\n### ***We welcome contributions!***\n\n### Quick Contribution Setup\n\n</div>\n\n```bash\n# Fork the repo, then:\ngit clone https://github.com/Miskler/pytest-jsonschema-snapshot.git\ncd jsonschema-diff\n# Install\nmake reinstall\n# Ensure everything works\nmake test\nmake lint\nmake type-check\n# After code editing\nmake format\n```\n\n<div align=\"center\">\n\n## \ud83d\udcc4 License\n\nMIT License - see [LICENSE](LICENSE) file for details.\n\n*Made with \u2764\ufe0f for developers working with evolving JSON schemas*\n\n</div>\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Pytest plugin for automatic JSON Schema generation and validation from examples",
    "version": "0.2.2",
    "project_urls": {
        "Documentation": "https://miskler.github.io/pytest-jsonschema-snapshot/basic/quick_start.html",
        "Homepage": "https://miskler.github.io/pytest-jsonschema-snapshot/basic/quick_start.html",
        "Issues": "https://github.com/Miskler/pytest-jsonschema-snapshot/issues",
        "Repository": "https://github.com/Miskler/pytest-jsonschema-snapshot"
    },
    "split_keywords": [
        "genson",
        " json",
        " pytest",
        " rich",
        " schema",
        " validation"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "00665a9857daf2562312ddede65661dbcb6fb03b4596348d03257332c6cf23e8",
                "md5": "a565126a137c0f459bb1bb54c20c5e3d",
                "sha256": "0fb4e45d22a28babc35a63b55944bd8493a9582310f051b94ab5e697767b8e8a"
            },
            "downloads": -1,
            "filename": "pytest_jsonschema_snapshot-0.2.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a565126a137c0f459bb1bb54c20c5e3d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 18774,
            "upload_time": "2025-09-02T00:38:35",
            "upload_time_iso_8601": "2025-09-02T00:38:35.652502Z",
            "url": "https://files.pythonhosted.org/packages/00/66/5a9857daf2562312ddede65661dbcb6fb03b4596348d03257332c6cf23e8/pytest_jsonschema_snapshot-0.2.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "36467d0b17ab82f46b6da15458c2585991311d731b70bf1b60e5a97fd70c1cbd",
                "md5": "637baf42de0902ed3d8318c59033e6b4",
                "sha256": "658ba605487a18e044d770b9c33971372e5d192e31fbb4b8c59ba846f090a319"
            },
            "downloads": -1,
            "filename": "pytest_jsonschema_snapshot-0.2.2.tar.gz",
            "has_sig": false,
            "md5_digest": "637baf42de0902ed3d8318c59033e6b4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 198096,
            "upload_time": "2025-09-02T00:38:37",
            "upload_time_iso_8601": "2025-09-02T00:38:37.341050Z",
            "url": "https://files.pythonhosted.org/packages/36/46/7d0b17ab82f46b6da15458c2585991311d731b70bf1b60e5a97fd70c1cbd/pytest_jsonschema_snapshot-0.2.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-09-02 00:38:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Miskler",
    "github_project": "pytest-jsonschema-snapshot",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "genson",
            "specs": []
        },
        {
            "name": "pathvalidate",
            "specs": []
        },
        {
            "name": "jsonschema",
            "specs": []
        },
        {
            "name": "jsonschema-diff",
            "specs": []
        },
        {
            "name": "pytest",
            "specs": []
        },
        {
            "name": "types-jsonschema",
            "specs": []
        },
        {
            "name": "pytest-asyncio",
            "specs": []
        },
        {
            "name": "pytest-cov",
            "specs": []
        },
        {
            "name": "pytest-xdist",
            "specs": []
        },
        {
            "name": "black",
            "specs": []
        },
        {
            "name": "isort",
            "specs": []
        },
        {
            "name": "mypy",
            "specs": []
        },
        {
            "name": "flake8",
            "specs": []
        },
        {
            "name": "sphinx",
            "specs": []
        },
        {
            "name": "sphinx-autoapi",
            "specs": []
        },
        {
            "name": "furo",
            "specs": []
        },
        {
            "name": "docutils",
            "specs": []
        },
        {
            "name": "types-docutils",
            "specs": []
        }
    ],
    "lcname": "pytest-jsonschema-snapshot"
}
        
Elapsed time: 1.33773s