I have lets say two dataclasses where one is used inside the other like this :
@dataclass(frozen=True)
class SensorModel:
sensor_id: int
type: str
health_status: bool
@dataclass
class SamplingModel:
trigger: str
priority: str = field(init=False)
time: datetime
sensors: List[SensorModel]
how can I use hypothesis to generate sample for my testing from this?
I have found in the docs that hypothesis strategies support dataclasses natively hypothesis but no examples ANYWHERE about how to do it in a simple case like the one described.
Ok so I found an answer, its turns out it is really simple you can use the from_type
generator from_type documentation
In the simple example I described above one could do something like
from hypothesis import given
from hypothesis.strategies import from_type
@dataclass(frozen=True)
class SensorModel:
sensor_id: int
type: str
health_status: bool
@dataclass
class SamplingModel:
trigger: str
priority: str = field(init=False)
time: datetime
sensors: List[SensorModel]
@given(sample=from_type(SamplingModel))
def test_samples(sample):
assert sample.trigger == sample.trigger #point-less test replace with your logic