Skip to main content

Generate test data from JSON schemata with Hypothesis

Project description

hypothesis-jsonschema

A Hypothesis strategy for generating data that matches some JSON schema.

Here's the PyPI page.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hypothesis-jsonschema-0.22.1.tar.gz (41.5 kB view details)

Uploaded Source

Built Distribution

hypothesis_jsonschema-0.22.1-py3-none-any.whl (28.6 kB view details)

Uploaded Python 3

File details

Details for the file hypothesis-jsonschema-0.22.1.tar.gz.

File metadata

File hashes

Hashes for hypothesis-jsonschema-0.22.1.tar.gz
Algorithm Hash digest
SHA256 5dd7449009f323e408a9aa64afb4d18bd1f60ea2eabf5bf152a510da728b34f2
MD5 af050ab33dd3a2a94298aebf7ba68161
BLAKE2b-256 8bd747d7c208ca0f8a2ce6535d6cf0a167d9120ccfc00d6c5ff5fae7936a4654

See more details on using hashes here.

Provenance

File details

Details for the file hypothesis_jsonschema-0.22.1-py3-none-any.whl.

File metadata

File hashes

Hashes for hypothesis_jsonschema-0.22.1-py3-none-any.whl
Algorithm Hash digest
SHA256 082968cb86a6aac2369627b08753cbf714c08054b1ebfce3588e3756e652cde6
MD5 83d2d8e57422d2bb14937a8ee503811f
BLAKE2b-256 b63fffbaaa089b06c3b62ac3e09cfcc36981103416627bd1e37f626b33bead88

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page