mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
771b0d3d92
* Configure Hypothesis * Hypothesis plugin docs * Add Hypothesis plugin Co-authored-by: Samuel Colvin <s@muelcolvin.com>
25 lines
780 B
Python
25 lines
780 B
Python
import typing
|
|
from hypothesis import given, strategies as st
|
|
from pydantic import BaseModel, EmailStr, PaymentCardNumber, PositiveFloat
|
|
|
|
|
|
class Model(BaseModel):
|
|
card: PaymentCardNumber
|
|
price: PositiveFloat
|
|
users: typing.List[EmailStr]
|
|
|
|
|
|
@given(st.builds(Model))
|
|
def test_property(instance):
|
|
# Hypothesis calls this test function many times with varied Models,
|
|
# so you can write a test that should pass given *any* instance.
|
|
assert 0 < instance.price
|
|
assert all('@' in email for email in instance.users)
|
|
|
|
|
|
@given(st.builds(Model, price=st.floats(100, 200)))
|
|
def test_with_discount(instance):
|
|
# This test shows how you can override specific fields,
|
|
# and let Hypothesis fill in any you don't care about.
|
|
assert 100 <= instance.price <= 200
|