Hypothesis is the Python library for
Hypothesis can infer how to construct type-annotated classes, and supports builtin types,
many standard library types, and generic types from the
typing_extensions modules by default.
From Pydantic v1.8 and Hypothesis v5.29.0,
Hypothesis will automatically load support for custom types like
PositiveFloat, so that the
strategies support them without any user configuration.
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
Use with JSON Schemas🔗
To test client-side code, you can use
Model.schema() with the
to generate arbitrary JSON instances matching the schema.
For web API testing, Schemathesis provides
a higher-level wrapper and can detect both errors and security vulnerabilities.