Skip to main content

pytest-pydantic-schema-sync

pytest-pydantic-schema-sync is a pytest plugin that automates the process of saving JSON schemas for Pydantic models during test execution. It builds upon the functionality provided by the pydantic-schema-sync package.

Features

  • Automatically syncs JSON schemas for Pydantic models during pytest execution
  • Configurable schema storage location (package root or repository root)
  • Easy setup using pytest markers and Enum-based configuration

Installation

You can install pytest-pydantic-schema-sync using pip:

pip install pytest-pydantic-schema-sync

Usage

Define an Enum class with your Pydantic model paths, marked with the pydantic_schema_sync marker:

from pytest import mark
from enum import Enum

@mark.pydantic_schema_sync
class ModelSchemas(Enum):
    user = "myapp.models.User"
    product = "myapp.models.Product"

Running your pytest suite will generate tests for each of these, which fail if the sync is unsuccessful. The plugin will automatically generate and sync JSON schemas for the specified models using pydantic-schema-sync.

Configuration

You can configure the plugin behavior in your pytest.ini or pyproject.toml file:

[tool.pytest.ini_options]
pydantic_schema_sync = {schema_location = "package_root", schema_dir = "schemas", repo_flatten = false, mjs_kwargs = {}}

These are the defaults, but it demonstrates use of JSON to configure the plugin.

  • schema_location: Where to store schema files. Options are "package_root" (default) or "repo_root".
    • repo_flatten: If "repo_root" is chosen, then if this option is set to True (default: False) all JSON files will be written to a single directory rather than in subfolders per-package.
  • schema_dir: Name of the directory to store schema files under whichever schema location root. Default is "schemas".
  • mjs_kwargs: Keyword arguments to pass to the Pydantic models' model_json_schema() method. Default is {} (i.e. do not override any kwargs).
    • A common kwarg you may wish to set is by_alias (default: True)

How It Works

The plugin uses the pydantic-schema-sync package to generate JSON schemas for your Pydantic models. It creates a separate test item for each model specified in your Enum, ensuring that schemas are synced even if no other tests are run for those models.

Example

Check out the simple-plugin-demo for a working example of how to use this plugin.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Metadata

Release files for pytest-pydantic-schema-sync 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-pydantic-schema-sync 0.2.1
File Size Uploaded
pytest_pydantic_schema_sync-0.2.1.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-pydantic-schema-sync 0.2.1
File Interpreter ABI Platform
pytest_pydantic_schema_sync-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.8 kB

Release files / pytest_pydantic_schema_sync-0.2.1.tar.gz

Download URL pytest_pydantic_schema_sync-0.2.1.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c10067dbcb08decc8b06f8efe40afe2d50310a2fc198c49fd4de1adb2afd4272
BLAKE2b-256 checksum
How to use checksums
fa73bc228ffd6a10ef71ba4c54744b0544966a5a1a8966a75e926f927635ddf7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.18.1 CPython/3.10.14 Linux/5.15.0-117-generic

Release files / pytest_pydantic_schema_sync-0.2.1-py3-none-any.whl

Download URL pytest_pydantic_schema_sync-0.2.1-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
54b94be98c099f1efc2318c3971080c6b52ca363133027e9a59a5a0b14ea7cf1
BLAKE2b-256 checksum
How to use checksums
08ee40ca93f016b6411ecba0d6e55f61e9ce71bda73924bc2ff623557f999c76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.18.1 CPython/3.10.14 Linux/5.15.0-117-generic

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page