跳转到主要内容

使用Hypothesis从JSON模式生成测试数据

项目描述

hypothesis-jsonschema

一个Hypothesis策略,用于生成符合某些JSON模式的数据。

这是PyPI页面。

API

公共API只包含一个函数:hypothesis_jsonschema.from_schema,它接受一个JSON模式并返回一个允许的JSON对象的策略。

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")

有关基于属性的测试和如何使用或自定义策略的更多详细信息,请参阅Hypothesis文档

JSONSchema草案04、05和07已完全测试并正常工作。从版本0.11开始,这包括解析非递归引用!

支持的版本

hypothesis-jsonschema需要Python 3.6或更高版本。通常,0.x版本将需要所有依赖项的最新版本,因为我不想处理兼容性工作。

hypothesis-jsonschema可能在1.x版本之前的任何时候进行不兼容的更改——这就是semver的含义!——但我已经将API表面保持得足够小,应该可以避免这种情况。更改的主要来源将是如果或当从未真正工作的模式变为显式错误,而不是生成不完全匹配的值。

您可以通过赞助我来获得优先支持、路线图输入和优先级功能开发。

hypothesis-jsonschema做出贡献

我们欢迎外部贡献——并试图让它们既容易又有趣。您可以在我们的贡献指南中了解更多细节,并查看GitHub上的所有贡献者。谢谢大家!

变更日志

补丁说明可以在CHANGELOG.md中找到。点击此处

安全联系方式

要报告安全漏洞,请使用Tidelift安全联系方式。Tidelift将协调修复和披露。

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

hypothesis-jsonschema-0.23.1.tar.gz (42.9 kB 查看哈希值)

上传时间

构建分布

hypothesis_jsonschema-0.23.1-py3-none-any.whl (29.2 kB 查看哈希值)

上传时间 Python 3

由以下支持