Generate test data from JSON schemata with Hypothesis
Project description
hypothesis-jsonschema
A Hypothesis strategy for generating data that matches some JSON schema.
API
The public API consists of just one function: hypothesis_jsonschema.from_schema
,
which takes a JSON schema and returns a strategy for allowed JSON objects.
from hypothesis import given
from hypothesis_jsonschema import from_schema
@given(from_schema({"type": "integer", "minimum": 1, "exclusiveMaximum": 10}))
def test_integers(value):
assert isinstance(value, int)
assert 1 <= value < 10
@given(
from_schema(
{"type": "string", "format": "card"},
# Standard formats work out of the box. Custom formats are ignored
# by default, but you can pass custom strategies for them - e.g.
custom_formats={"card": st.sampled_from(EXAMPLE_CARD_NUMBERS)},
)
)
def test_card_numbers(value):
assert isinstance(value, str)
assert re.match(r"^\d{4} \d{4} \d{4} \d{4}$", value)
For more details on property-based testing and how to use or customise strategies, see the Hypothesis docs.
JSONSchema drafts 04, 05, and 07 are fully tested and working. As of version 0.11, this includes resolving non-recursive references!
Supported versions
hypothesis-jsonschema
requires Python 3.6 or later.
In general, 0.x versions will require very recent versions of all dependencies
because I don't want to deal with compatibility workarounds.
hypothesis-jsonschema
may make backwards-incompatible changes at any time
before version 1.x - that's what semver means! - but I've kept the API surface
small enough that this should be avoidable. The main source of breaks will be
if or when schema that never really worked turn into explicit errors instead
of generating values that don't quite match.
You can sponsor me to get priority support, roadmap input, and prioritized feature development.
Contributing to hypothesis-jsonschema
We love external contributions - and try to make them both easy and fun. You can read more details in our contributing guide, and see everyone who has contributed on GitHub. Thanks, everyone!
Changelog
Patch notes can be found in CHANGELOG.md
.
Security contact information
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
Project details
Release history Release notifications | RSS feed
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 hypothesis-jsonschema-0.18.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 596b6d518a8290c6566cd364b1ede07b1be8df8b2a1f09dc158f683654c23829 |
|
MD5 | 0a95d0de98bb553185cb7f631a18d9c3 |
|
BLAKE2b-256 | 93f1ccb8de252c70830276a3da794a3a0c8c970b1b502291542f5a8e1b428b6e |
Hashes for hypothesis_jsonschema-0.18.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12d3de0d423848625e05b1367bbb0d87d65f0b80d2c00a17246e37e34b63fad8 |
|
MD5 | 17bf5f19513a455a1b2142caf4f9f11d |
|
BLAKE2b-256 | e9cc7a80f17660daca352587fb99b11ba3cad40b08680e53f5f62f4966d2ef55 |