0

I'm new to Hypothesis and I want to test a function which takes integer input from Hypothesis Strategy:

@given(strategy.integers(min_value=2, max_value=9))
def test_function(t):
    #...

Hypothesis tests the function starting from min_value 2 to 9.

Is there a way I could reverse this? I want the test cases to start with t=9 instead of t=2.

Aniket
  • 11
  • 4

2 Answers2

1

Usually strategies choose their values randomly. So your properties aka test cases should never depend on a certain order of values.

Most of the time each invocation of the test method should be fully independent of any previous or later invocation. If the way you test requires a dependency please tell us more about what and how you’re testing so that a testing approach can be recommended.

johanneslink
  • 4,877
  • 1
  • 20
  • 37
  • Thank you for your reply. I want to find the minimum delay required for an assertion to succeed after an interface call has been made. So I want the test case to start from a max_value of time and proceed towards min_value until it starts failing. As of now, as the test case starts with min_value, it obviously fails at t=0 or t=2. Is there any way I could find the boundary value of time using strategy then? – Aniket Oct 19 '19 at 09:58
  • Also, there is no dependency between test method invocations. – Aniket Oct 19 '19 at 10:00
  • I think I wouldn't use Hypothesis for this purpose. I'd rather count down the delay myself and run the test method from main. – johanneslink Oct 19 '19 at 13:11
  • I wanted to make use of Hypothesis and property based testing in general for this purpose. So there is no other way is it? – Aniket Oct 19 '19 at 13:30
  • 1
    As far as I understand you haven’t found a property yet that you could check using PBT. The test you’re describing serves to find a threshold as a candidate for a property’s invariant. This is different from coming up with an invariant that should hold and let Hypothesis find out if it’s true.That is IMO the reason for Hypothesis being not a good fit here. But maybe I got your intentions wrong? – johanneslink Oct 19 '19 at 20:57
  • Oh I understand it now. I had learnt from research papers that automatic test case generation is a significant part of PBT and because of this I concentrated only on that and got the concept of property in my test case wrong. Thank you so much for pointing the mistake out. Appreciate it. – Aniket Oct 19 '19 at 21:42
0

Hypothesis tests the function starting from min_value 2 to 9.

Nope, Hypothesis tests that minimal input first and then other inputs in a random order.

If the order of test cases matters, an explicit loop is a better choice than property-based testing.

Zac Hatfield-Dodds
  • 2,455
  • 6
  • 19