0

I am writing a custom search strategy with builds() (this doesn't matter w.r.t. this question) which shall use hypothesis.strategies.integers(min_value=None, max_value=None) to generate integer data with an explicit step size other than, let's say delta 10. I do not need a list of values like [10, 20, 30, 40, etc.]. Instead I need subsequent calls of the test function to be called with integer values with step size of 10, e.g. with 10 for the first call, 20 for the second call, etc. How can I achieve this easiest?

thinwybk
  • 4,193
  • 2
  • 40
  • 76

1 Answers1

1

You can easily adapt existing strategies, for example generating even numbers via:

integers().map(lambda x: x * 2)

And just to check - are you using a recent version of Hypothesis? You linked to the documentation for v1.8, which is unsupported and significantly less powerful than the current version 3.48.

Finally, consider a composite strategy if you need to have a particular relationship between the parts of whatever you're constructing - builds() is simpler but doesn't support dependencies between arguments.


I need subsequent calls of the test function to be called with integer values with step size of 10, e.g. with 10 for the first call, 20 for the second call, etc.

Hypothesis only supports stateful testing via the hypothesis.stateful module.

By design, each example provided by @given is independent of any other - if this doesn't work for your use case Hypothesis is probably the wrong tool for the job.

Zac Hatfield-Dodds
  • 2,455
  • 6
  • 19
  • Thanks, I fixed the link to latest docs. I don't think that `@composite` helps in this situation. I should be just fine with `builds()`. Your example applies to the scope of the current value generated only I guess. But I need to relate to previously generated values... – thinwybk Mar 06 '18 at 09:10
  • All examples I found so far use `@given(st.integers().filter(lambda x: ...)` like the example from [here](http://hypothesis.readthedocs.io/en/latest/details.html?highlight=x%5B0%5D#test-statistics)... – thinwybk Mar 06 '18 at 09:55
  • Filtering is more general, but much less efficient and won't shrink as well. That example you link to is showing how the impact of filtering is reported - ~80% of events means a five times slowdown! I'm not sure what you mean by "I need to relate to previously generated values" - please expand on this in your question. – Zac Hatfield-Dodds Mar 10 '18 at 12:20
  • All examples I found relate to the type in general only. E.g. `@given(st.integers().filter(lambda x: x % 2 == 0))` filters for even integers. However I need to either filter or whatever works instead with kind of pseudo code like this `@given(st.integers().filter(lambda x: x[t] = x[t-1] +/- 1))`. – thinwybk Mar 10 '18 at 14:15
  • I have no idea what you're trying to do with the psudeocode - you can't assign in a lambda, index into an integer (`x`), and I don't know what `t` is. If you want a **list of integers** with a particular relationship between elements that's much easier, and *you should edit your question accordingly*. – Zac Hatfield-Dodds Mar 12 '18 at 05:47
  • I am improving the question. This should make the comments above obsolete. – thinwybk Mar 12 '18 at 07:45
  • Nice to see the `hypothesis` developer on SO! – hoefling Mar 17 '18 at 11:58