pytest-jsonschema

A pytest plugin to perform JSONSchema validations


Keywords
pytest, testing
License
Other
Install
pip install pytest-jsonschema==1.0.0a2

Documentation

pytest-jsonschema

PyPI Python Version Wheel License Status Tests Linters Code Style Contributors Stars

pytest-jsonschema is a plugin for pytest designed to facilitate JSON Schema validations within your test suites. This tool enables you to validate JSON files, strings, and Python objects against predefined JSON Schemas, ensuring your data adheres to expected formats.

Installation

Install pytest-jsonschema using pip from PyPI:

pip install pytest-jsonschema

Features & Usage

The package introduces three pytest fixtures for validating JSON data:

schema_validate_file

Validates a JSON file located in your test suite directory:

from pathlib import Path

def test_package_json_is_valid(schema_validate_file):
    path = Path("package.json")
    assert schema_validate_file(path=path, schema_name="package")

schema_validate_string

Validates a JSON string:

from pathlib import Path

def test_package_json_is_valid(schema_validate_string):
    data = Path("package.json").read_text()
    assert schema_validate_string(data=data, schema_name="package", file_type="json")

schema_validate

Validates a Python dictionary representing JSON data:

import json
from pathlib import Path

def test_package_json_is_valid(schema_validate):
    data = json.loads(Path("package.json").read_text())
    assert schema_validate(data=data, schema_name="package")

Requirements

  • pytest >= 6.2.0

Contributing

To contribute to pytest-jsonschema, please follow these steps:

  1. Clone the repository:
git clone git@github.com:collective/pytest-jsonschema.git
  1. Install the package for development:
make install
  1. Format the codebase:
make format
  1. Run tests:
  • To run all tests:
    make test
    
  • To stop on the first error and open a pdb session:
    make debug-test
    

Testing is conducted using pytest and tox.

License 📜

pytest-jsonschema is licensed under the MIT License.