Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Tools to generate test data from JSON schemata with Hypothesis

License

NotificationsYou must be signed in to change notification settings

python-jsonschema/hypothesis-jsonschema

Repository files navigation

AHypothesis strategy for generating datathat matches someJSON 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.

fromhypothesisimportgivenfromhypothesis_jsonschemaimportfrom_schema@given(from_schema({"type":"integer","minimum":1,"exclusiveMaximum":10}))deftest_integers(value):assertisinstance(value,int)assert1<=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)},    ))deftest_card_numbers(value):assertisinstance(value,str)assertre.match(r"^\d{4} \d{4} \d{4} \d{4}$",value)@given(from_schema({},allow_x00=False,codec="utf-8").map(json.dumps))deftest_card_numbers(payload):assertisinstance(payload,str)assert"\0"notinpayload# use allow_x00=False to exclude null characters# If you want to restrict generated strings characters which are valid in# a specific character encoding, you can do that with the `codec=` argument.payload.encode(codec="utf-8")

For more details on property-based testing and how to use or customisestrategies,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 dependenciesbecause I don't want to deal with compatibility workarounds.

hypothesis-jsonschema may make backwards-incompatible changes at any timebefore version 1.x - that's what semver means! - but I've kept the API surfacesmall enough that this should be avoidable. The main source of breaks will beif or when schema that never really worked turn into explicit errors insteadof generating values that don't quite match.

You cansponsor me to get prioritysupport, roadmap input, and prioritized feature development.

Contributing tohypothesis-jsonschema

We love external contributions - and try to make them both easy and fun.You canread more details in our contributing guide,andsee everyone who has contributed on GitHub.Thanks, everyone!

Changelog

Patch notescan be found inCHANGELOG.md.

Security contact information

To report a security vulnerability, please use theTidelift security contact.Tidelift will coordinate the fix and disclosure.

About

Tools to generate test data from JSON schemata with Hypothesis

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Contributors10

Languages


[8]ページ先頭

©2009-2025 Movatter.jp