Pytest plugin to synchronise Pydantic model schemas with JSONSchema files
Project description
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}
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".
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.
Project details
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
Hashes for pytest_pydantic_schema_sync-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ff3539e922659cb24ca3929173c884075f31024c9f30b9f4663cd273e2bd29b |
|
MD5 | 25e66660487431ea343f9223a9440d88 |
|
BLAKE2b-256 | a6430664b15ed064291960d57b54fad1125bcea9609ee963ef1f2f201927e5f0 |
Hashes for pytest_pydantic_schema_sync-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21e864ddd21a5c34cccdb9cbac34d7c7b3fe0d9271fc9067c20c2bd353be1e45 |
|
MD5 | 294072fd73941e9ebba1d24b90755cf6 |
|
BLAKE2b-256 | 866f76a9e662205f16a91353bccb4b0dd1f82585d29d377b42ea252ae28bc41b |