I am using hypothesis for testing, and I wanted to establish a relationship between two arguments of a test. I am aware of assume
, but that seems quite wasteful when I know the constraints beforehand.
Here's a minimal example:
from datetime import date
import pytest
from hypothesis import given, assume, strategies as st
def get_daterange_filter(start, end):
"""`start` and `end` are dates of the format: YYYYMMDD"""
if int(start) > int(end):
raise ValueError(f"{start} comes after {end}")
else:
pass
dt_strategy = st.dates(min_value=date(2019, 4, 1),
max_value=date(2019, 7, 31))
@given(dt_strategy, dt_strategy)
def test_daterange_filter(dt1, dt2):
assume(dt1 > dt2)
start, end = dt1.strftime("%Y%m%d"), dt2.strftime("%Y%m%d")
with pytest.raises(ValueError):
get_daterange_filter(start, end)
The statistics summary for the above reports the following:
hypo.py::test_daterange_filter:
- 100 passing examples, 0 failing examples, 68 invalid examples
- Typical runtimes: 0-1 ms
- Fraction of time spent in data generation: ~ 47%
- Stopped because settings.max_examples=100
That's quite a few wasted attempts. This is a very simple case, but in a typical data heavy project I can foresee many such scenarios. So I was wondering, if there's an easy way to tell hypothesis that the two arguments satisfy a certain relationship (in this case, one greater than the other). I was unable to find anything in the docs.