跳转到主要内容

基于Lollipop schema使用Hypothesis生成随机测试数据的库

项目描述

License: MIT Build Status PyPI

基于Lollipop schema使用Hypothesis生成随机测试数据的库。

示例

from collections import namedtuple
import lollipop.types as lt
import lollipop.validators as lv
import string

EMAIL_REGEXP = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]{2,}\.[a-zA-Z0-9-.]{2,}$"
Email = lt.validated_type(lt.String, 'Email', lv.Regexp(EMAIL_REGEXP))

User = namedtuple('User', ['name', 'email', 'age'])

USER = lt.Object({
    'name': lt.String(validate=lv.Length(min=1)),
    'email': Email(),
    'age': lt.Optional(lt.Integer(validate=lv.Range(min=18))),
}, constructor=User)

import hypothesis as h
import hypothesis.strategies as hs
import lollipop_hypothesis as lh

# Write a test using data generation strategy based on Lollipop schema
@h.given(lh.type_strategy(USER))
def test_can_register_any_valid_user(user):
    register(user)

# Configure custom strategy for Email type
lh.register(
    Email,
    lambda _, type, context=None: \
        hs.tuples(
            hs.text('abcdefghijklmnopqrstuvwxyz'
                    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                    '0123456789'
                    '_.+-', min_size=1),
            hs.lists(
                hs.text('abcdefghijklmnopqrstuvwxyz'
                        'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                        '0123456789', min_size=2),
                min_size=2,
                average_size=3,
            )
        ).map(lambda (name, domain_parts): name + '@' + '.'.join(domain_parts)),
)

# Or configure custom strategy for the whole type instance
lh.register(
    USER,
    lambda registry, type, context=None: \
        hs.builds(
            User,
            name=hs.text(min_size=1),
            email=registry.convert(Email(), context),
            age=hs.integers(min_value=0, max_value=100),
        )
)

安装

$ pip install lollipop-hypothesis

# install optional package for regex support
$ pip install lollipop-hypothesis[regex]

要求

许可证

MIT许可。有关详细信息,请参阅附带LICENSE文件。

项目详情


下载文件

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

源分布

lollipop-hypothesis-0.2.tar.gz (7.2 kB 查看散列值)

上传时间 源代码

构建分发

lollipop_hypothesis-0.2-py2.py3-none-any.whl (7.1 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下提供支持

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误日志StatusPageStatusPage状态页面