0

hypothesis很多策略,我仍在努力理解它们。看看它们产生了哪些价值对我有很大帮助。那可能吗?

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列表中。有没有办法将策略用作生成器/生成策略测试的值列表?

4

1 回答 1

1

我建议打开verbosity设置,它将打印假设为您的测试生成的所有示例。

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

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

于 2020-07-08T12:24:02.863 回答