使用Hypothesis从JSON模式生成测试数据
项目描述
hypothesis-jsonschema
一个Hypothesis策略,用于生成符合某些JSON模式的数据。
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.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | f4ac032024342a4149a10253984f5a5736b82b3fe2afb0888f3834a31153f215 |
|
MD5 | 240b837b4f6793d14a46cc54a3e88bb9 |
|
BLAKE2b-256 | 4fad2073dd29d8463a92c243d0c298370e50e0d4082bc67f156dc613634d0ec4 |
关闭
哈希值 for hypothesis_jsonschema-0.23.1-py3-none-any.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a4d74d9516dd2784fbbae82e009f62486c9104ac6f4e3397091d98a1d5ee94a2 |
|
MD5 | 0bfb4d974701af5f61b54afab847e3df |
|
BLAKE2b-256 | 1744635a8d2add845c9a2d99a93a379df77f7e70829f0a1d7d5a6998b61f9d01 |