Registry / testing / pytest-schema

pytest-schema

JSON →
library0.1.2pypypi✓ verified 87d ago

A pytest plugin that allows you to validate return values against a schema-like object, built on top of the `schema` library. Current version 0.1.2, released June 2024. Low release cadence.

pip install pytest-schema
INSTALL
IMPORT
SIG · PYTEST-SCHEMA
P
pytest-schema
testingpythonv0.1.2
Install
2.9s avg
Import
34ms
Disk
29MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.036s · 30.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.9s · import 0.032s · 31MB
29MB installed
● package 29MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

schema
from pytest_schema import schema
Direct import from pytest_schema
like_schema
from pytest_schema import like_schema
Alias for like method
exact_schema
from pytest_schema import exact_schema
Alias for exact method

Basic usage of schema, like_schema, and exact_schema in pytest tests.

from pytest_schema import schema def test_validate_schema(): expected = {"name": str, "age": int} actual = {"name": "Alice", "age": 30} assert actual == schema(expected) def test_like_schema(): from pytest_schema import like_schema expected = {"id": int, "value": str} actual = {"id": 1, "value": "foo", "extra": True} assert actual == like_schema(expected) def test_exact_schema(): from pytest_schema import exact_schema expected = {"key": str} actual = {"key": "val"} assert actual == exact_schema(expected)
Debug
Known issues
gotchaThe `schema` callable returns a validator that must be compared with `==` (or `!=`) to actually validate. Simply calling `schema(...)` without comparison does nothing.
fix
Always use `assert actual == schema(expected)` or `assert actual != schema(expected)`.
affects: all
gotcha`like_schema` (or `like`) allows extra keys in the actual dict, while `exact_schema` (or `exact`) does not. Using the wrong one can cause false passes or failures.
fix
Use `like_schema` when extra keys are acceptable; use `exact_schema` when the dict must match exactly.
affects: all
deprecatedThe methods `like` and `exact` are deprecated in favor of `like_schema` and `exact_schema`. The old names may be removed in future versions.
fix
Use `like_schema` and `exact_schema` instead.
affects: >=0.1.1
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pytest_schema'
pytest-schema is not installed or installed in a different environment.
fix
Run `pip install pytest-schema` in your virtual environment.
TypeError: 'Schema' object is not callable
Using the `schema` object from `schema` library directly instead of pytest_schema's wrapper.
fix
Import `schema` from `pytest_schema` instead: `from pytest_schema import schema`.
AssertionError: assert <pytest_schema...> == ...
The schema comparison failed because the actual data does not match the schema.
fix
Check the types and structure of your actual data against the schema definition.
Upgrade
Version history
0.1.2latest on PyPI · released Feb 16, 2024
Audit
Dependencies
pytestrequiredCore testing framework
schemarequiredSchema validation library
Agent activity
6 hits · last 30 days
node
6
Resources
pytest-schema — pip install pytest-schema · libregistry