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.17.3.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hypothesis_jsonschema-0.17.3-py3-none-any.whl (25.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hypothesis-jsonschema-0.17.3.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for hypothesis-jsonschema-0.17.3.tar.gz
Algorithm Hash digest
SHA256 75d71bfb00535ca6cabdbb5e726e6aa6083ac35ece32a23d810aa8775701ee53
MD5 8e3e4f6923b362871c10c6ba21fc95ea
BLAKE2b-256 d7a63ca6870e7b27e1b1b67e2da4c851f34ea39ca24ac5de4a1afb601145b906

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hypothesis_jsonschema-0.17.3-py3-none-any.whl
  • Upload date:
  • Size: 25.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.3

File hashes

Hashes for hypothesis_jsonschema-0.17.3-py3-none-any.whl
Algorithm Hash digest
SHA256 857bd5124473e10dd4a7e4920c6c93d9b7e933e667850bfa8945d5c98eefdb19
MD5 8b28a1282c46f4511c878d545752e2d1
BLAKE2b-256 158a1613d806a0d689b2339331aa6666e4f6e52a8c8a6dbc07f011cac996fc2d

See more details on using hashes here.

Supported by

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