hypothesis-jsonschema


Namehypothesis-jsonschema JSON
Version 0.23.1 PyPI version JSON
download
home_pagehttps://github.com/Zac-HD/hypothesis-jsonschema
SummaryGenerate test data from JSON schemata with Hypothesis
upload_time2024-02-28 20:33:50
maintainer
docs_urlNone
authorZac Hatfield-Dodds
requires_python>=3.8
licenseMPL 2.0
keywords python testing fuzzing property-based-testing json-schema
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # hypothesis-jsonschema

A [Hypothesis](https://hypothesis.readthedocs.io) strategy for generating data
that matches some [JSON schema](https://json-schema.org/).

[Here's the PyPI page.](https://pypi.org/project/hypothesis-jsonschema/)

## 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.

```python
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)


@given(from_schema({}, allow_x00=False, codec="utf-8").map(json.dumps))
def test_card_numbers(payload):
    assert isinstance(payload, str)
    assert "\0" not in payload  # 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 customise
strategies, [see the Hypothesis docs](https://hypothesis.readthedocs.io/).

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](https://github.com/sponsors/Zac-HD) 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](https://github.com/Zac-HD/hypothesis-jsonschema/blob/master/CONTRIBUTING.md),
and [see everyone who has contributed on GitHub](https://github.com/Zac-HD/hypothesis-jsonschema/graphs/contributors).
Thanks, everyone!


### Changelog

Patch notes [can be found in `CHANGELOG.md`](https://github.com/Zac-HD/hypothesis-jsonschema/blob/master/CHANGELOG.md).


### Security contact information
To report a security vulnerability, please use the
[Tidelift security contact](https://tidelift.com/security).
Tidelift will coordinate the fix and disclosure.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Zac-HD/hypothesis-jsonschema",
    "name": "hypothesis-jsonschema",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "python testing fuzzing property-based-testing json-schema",
    "author": "Zac Hatfield-Dodds",
    "author_email": "zac@zhd.dev",
    "download_url": "https://files.pythonhosted.org/packages/4f/ad/2073dd29d8463a92c243d0c298370e50e0d4082bc67f156dc613634d0ec4/hypothesis-jsonschema-0.23.1.tar.gz",
    "platform": null,
    "description": "# hypothesis-jsonschema\n\nA [Hypothesis](https://hypothesis.readthedocs.io) strategy for generating data\nthat matches some [JSON schema](https://json-schema.org/).\n\n[Here's the PyPI page.](https://pypi.org/project/hypothesis-jsonschema/)\n\n## API\n\nThe public API consists of just one function: `hypothesis_jsonschema.from_schema`,\nwhich takes a JSON schema and returns a strategy for allowed JSON objects.\n\n```python\nfrom hypothesis import given\n\nfrom hypothesis_jsonschema import from_schema\n\n\n@given(from_schema({\"type\": \"integer\", \"minimum\": 1, \"exclusiveMaximum\": 10}))\ndef test_integers(value):\n    assert isinstance(value, int)\n    assert 1 <= value < 10\n\n\n@given(\n    from_schema(\n        {\"type\": \"string\", \"format\": \"card\"},\n        # Standard formats work out of the box.  Custom formats are ignored\n        # by default, but you can pass custom strategies for them - e.g.\n        custom_formats={\"card\": st.sampled_from(EXAMPLE_CARD_NUMBERS)},\n    )\n)\ndef test_card_numbers(value):\n    assert isinstance(value, str)\n    assert re.match(r\"^\\d{4} \\d{4} \\d{4} \\d{4}$\", value)\n\n\n@given(from_schema({}, allow_x00=False, codec=\"utf-8\").map(json.dumps))\ndef test_card_numbers(payload):\n    assert isinstance(payload, str)\n    assert \"\\0\" not in payload  # use allow_x00=False to exclude null characters\n    # If you want to restrict generated strings characters which are valid in\n    # a specific character encoding, you can do that with the `codec=` argument.\n    payload.encode(codec=\"utf-8\")\n```\n\nFor more details on property-based testing and how to use or customise\nstrategies, [see the Hypothesis docs](https://hypothesis.readthedocs.io/).\n\nJSONSchema drafts 04, 05, and 07 are fully tested and working.\nAs of version 0.11, this includes resolving non-recursive references!\n\n\n## Supported versions\n\n`hypothesis-jsonschema` requires Python 3.6 or later.\nIn general, 0.x versions will require very recent versions of all dependencies\nbecause I don't want to deal with compatibility workarounds.\n\n`hypothesis-jsonschema` may make backwards-incompatible changes at any time\nbefore version 1.x - that's what semver means! - but I've kept the API surface\nsmall enough that this should be avoidable.  The main source of breaks will be\nif or when schema that never really worked turn into explicit errors instead\nof generating values that don't quite match.\n\nYou can [sponsor me](https://github.com/sponsors/Zac-HD) to get priority\nsupport, roadmap input, and prioritized feature development.\n\n\n## Contributing to `hypothesis-jsonschema`\n\nWe love external contributions - and try to make them both easy and fun.\nYou can [read more details in our contributing guide](https://github.com/Zac-HD/hypothesis-jsonschema/blob/master/CONTRIBUTING.md),\nand [see everyone who has contributed on GitHub](https://github.com/Zac-HD/hypothesis-jsonschema/graphs/contributors).\nThanks, everyone!\n\n\n### Changelog\n\nPatch notes [can be found in `CHANGELOG.md`](https://github.com/Zac-HD/hypothesis-jsonschema/blob/master/CHANGELOG.md).\n\n\n### Security contact information\nTo report a security vulnerability, please use the\n[Tidelift security contact](https://tidelift.com/security).\nTidelift will coordinate the fix and disclosure.\n",
    "bugtrack_url": null,
    "license": "MPL 2.0",
    "summary": "Generate test data from JSON schemata with Hypothesis",
    "version": "0.23.1",
    "project_urls": {
        "Funding": "https://github.com/sponsors/Zac-HD",
        "Homepage": "https://github.com/Zac-HD/hypothesis-jsonschema"
    },
    "split_keywords": [
        "python",
        "testing",
        "fuzzing",
        "property-based-testing",
        "json-schema"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1744635a8d2add845c9a2d99a93a379df77f7e70829f0a1d7d5a6998b61f9d01",
                "md5": "0bfb4d974701af5f61b54afab847e3df",
                "sha256": "a4d74d9516dd2784fbbae82e009f62486c9104ac6f4e3397091d98a1d5ee94a2"
            },
            "downloads": -1,
            "filename": "hypothesis_jsonschema-0.23.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0bfb4d974701af5f61b54afab847e3df",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 29200,
            "upload_time": "2024-02-28T20:33:48",
            "upload_time_iso_8601": "2024-02-28T20:33:48.744067Z",
            "url": "https://files.pythonhosted.org/packages/17/44/635a8d2add845c9a2d99a93a379df77f7e70829f0a1d7d5a6998b61f9d01/hypothesis_jsonschema-0.23.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4fad2073dd29d8463a92c243d0c298370e50e0d4082bc67f156dc613634d0ec4",
                "md5": "240b837b4f6793d14a46cc54a3e88bb9",
                "sha256": "f4ac032024342a4149a10253984f5a5736b82b3fe2afb0888f3834a31153f215"
            },
            "downloads": -1,
            "filename": "hypothesis-jsonschema-0.23.1.tar.gz",
            "has_sig": false,
            "md5_digest": "240b837b4f6793d14a46cc54a3e88bb9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 42896,
            "upload_time": "2024-02-28T20:33:50",
            "upload_time_iso_8601": "2024-02-28T20:33:50.209721Z",
            "url": "https://files.pythonhosted.org/packages/4f/ad/2073dd29d8463a92c243d0c298370e50e0d4082bc67f156dc613634d0ec4/hypothesis-jsonschema-0.23.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-28 20:33:50",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Zac-HD",
    "github_project": "hypothesis-jsonschema",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "hypothesis-jsonschema"
}
        
Elapsed time: 3.85857s