1

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.

suvayu
  • 4,271
  • 2
  • 29
  • 35

1 Answers1

1

If you need interdependent strategies, make use of the strategy sharing:

dates = st.shared(st.dates(min_value=date(2019, 4, 1), max_value=date(2019, 7, 31)))

@st.composite
def later_dates(draw):
    return draw(st.dates(min_value=draw(dates)))
    # or, if you need the strict inequality for passing
    # the test in its given form, add a day to min_value:
    # return draw(st.dates(min_value=draw(dates) + timedelta(days=1)))


@given(start=later_dates(), end=dates)
def test_daterange_filter(start, end):
    fstart, fend = start.strftime("%Y%m%d"), end.strftime("%Y%m%d")
    with pytest.raises(ValueError):
        get_daterange_filter(fstart, fend)

Running more examples with time recorded:

...
collected 2 items

test_spam.py::test_daterange_filter PASSED
test_spam.py::test_daterange_filter_shared_date_strategy PASSED
======================================== Hypothesis Statistics =========================================

test_spam.py::test_daterange_filter:

  - 10000 passing examples, 0 failing examples, 10050 invalid examples
  - Typical runtimes: < 1ms
  - Fraction of time spent in data generation: ~ 44%
  - Stopped because settings.max_examples=10000

test_spam.py::test_daterange_filter_shared_date_strategy:

  - 10000 passing examples, 0 failing examples, 0 invalid examples
  - Typical runtimes: < 1ms
  - Fraction of time spent in data generation: ~ 50%
  - Stopped because settings.max_examples=10000

======================================== slowest test durations ========================================
12.55s call     test_spam.py::test_daterange_filter
6.27s call     test_spam.py::test_daterange_filter_shared_date_strategy

(0.00 durations hidden.  Use -vv to show these durations.)
====================================== 2 passed in 18.86 seconds =======================================
hoefling
  • 59,418
  • 12
  • 147
  • 194