我可以列出假设策略生成的对象吗?

时间:2020-06-28 11:25:59

标签: python-hypothesis

hypothesisa lot of strategies,但我仍在努力理解它们。查看它们生成哪些值将对我有很大帮助。有可能吗?

MVCE

使用hypothesis==5.18.3pydantic==1.5.1

from typing import Optional

from hypothesis import given
from hypothesis.strategies import from_type
from pydantic import BaseModel


class Adress(BaseModel):
    city: str
    street: str
    house_number: int
    postal_code: int


class Person(BaseModel):
    prename: str
    middlename: Optional[str]
    lastname: str
    address: Adress


@given(from_type(Person))
def test_me(person: Person):
    seen = [
        Person(
            prename="",
            middlename=None,
            lastname="",
            address=Adress(city="", street="", house_number=0, postal_code=0),
        ),
        Person(
            prename="0",
            middlename=None,
            lastname="",
            address=Adress(city="", street="", house_number=0, postal_code=0),
        ),
        Person(
            prename="",
            middlename=None,
            lastname="0",
            address=Adress(city="", street="", house_number=0, postal_code=0),
        ),
        Person(
            prename="",
            middlename=None,
            lastname="",
            address=Adress(city="", street="0", house_number=0, postal_code=0),
        ),
    ]
    assert person in seen

如您所见,我目前弄清假设的方式是将其手动添加到此seen列表中。有没有办法将策略用作生成器/生成策略测试的值列表?

1 个答案:

答案 0 :(得分:0)

我建议使用turning up the verbosity setting,它将打印假设为测试生成的所有示例。

如果您使用的是pytest,则还需要禁用输出捕获:pytest -s --hypothesis-verbosity=verbose MY_TEST_HERE

或者,在交互式会话中,您可以对策略对象调用.example()方法以获取任意示例。