| 1 | --- |
| 2 | tags: technical, details, faq, python |
| 3 | date: 2016-08-31 00:00 |
| 4 | title: How many times will Hypothesis run my test? |
| 5 | author: drmaciver |
| 6 | --- |
| 7 | |
| 8 | This is one of the most common first questions about Hypothesis. |
| 9 | |
| 10 | People generally assume that the number of tests run will depend on |
| 11 | the specific strategies used, but that's generally not the case. |
| 12 | Instead Hypothesis has a fairly fixed set of heuristics to determine |
| 13 | how many times to run, which are mostly independent of the data |
| 14 | being generated. |
| 15 | |
| 16 | But how many runs is *that*? |
| 17 | |
| 18 | The short answer is 200. Assuming you have a default configuration |
| 19 | and everything is running smoothly, Hypothesis will run your test |
| 20 | 200 times. |
| 21 | |
| 22 | The longer answer is "It's complicated". It will depend on the exact |
| 23 | behaviour of your tests and the value of some settings. In this article |
| 24 | I'll try to clear up some of the specifics of which |
| 25 | [settings](http://hypothesis.readthedocs.io/en/latest/settings.html) |
| 26 | affect the answer and how. |
| 27 | |
| 28 | <!--more--> |
| 29 | |
| 30 | Advance warning: This is a set of heuristics built up over time. It's |
| 31 | probably not the best choice of heuristics, but it mostly seems |
| 32 | to work well in practice. It will hopefully be replaced with a |
| 33 | simpler set of rules at some point. |
| 34 | |
| 35 | The first setting that affects how many times the test function will |
| 36 | be called is the timeout setting. This specifies a maximum amount of |
| 37 | time for Hypothesis to run your tests for. Once that has exceeded it |
| 38 | will stop and not run any more (note: This is a soft limit, so it |
| 39 | won't interrupt a test midway through). |
| 40 | |
| 41 | The result of this is that slow tests may get run fewer times. By |
| 42 | default the timeout is one minute, which is high enough that most |
| 43 | tests shouldn't hit it, if your tests take somewhere in the region |
| 44 | of 300-400ms on average they will start to hit the timeout. |
| 45 | |
| 46 | The timeout is respected regardless of whether the test passes or |
| 47 | fails, but other than that the behaviour for a passing test is |
| 48 | very different from a failing one. |
| 49 | |
| 50 | ### Passing tests |
| 51 | |
| 52 | For the passing case there are two other settings that affect the |
| 53 | answer: max\_examples and max\_iterations. |
| 54 | |
| 55 | In the normal case, max\_examples is what you can think of as the |
| 56 | number of test runs. The difference comes when you start using |
| 57 | assume or filter (and a few other cases). |
| 58 | |
| 59 | Hypothesis distinguishes between a *valid* test run and an invalid one |
| 60 | - if assume has been called with a falsey value or at some point in |
| 61 | the generation process it got stuck (e.g. because filter couldn't |
| 62 | find any satisfying examples) it aborts the example and starts |
| 63 | again from the beginning. max\_examples counts only valid examples |
| 64 | while max\_iterations counts all examples, valid or otherwise. Some |
| 65 | duplicate tests will also be considered invalid (though Hypothesis |
| 66 | can't distinguish all duplicates. e.g. if you did |
| 67 | integers().map(lambda x: 1) it would think you had many distinct values |
| 68 | when you only had one). The default value for max_iterations |
| 69 | is currently 1000. |
| 70 | |
| 71 | To see why it's important to have the max\_iterations limit, |
| 72 | consider something like: |
| 73 | |
| 74 | ```python |
| 75 | from hypothesis import assume, given, strategies as st |
| 76 | |
| 77 | |
| 78 | @given(st.integers()) |
| 79 | def test_stuff(i): |
| 80 | assume(False) |
| 81 | ``` |
| 82 | |
| 83 | Then without a limit on invalid examples this would run forever. |
| 84 | |
| 85 | Conversely however, treating valid examples specially is useful because |
| 86 | otherwise even casual use of assume would reduce the number of tests |
| 87 | you run, reducing the quality of your testing. |
| 88 | |
| 89 | Another thing to note here is that the test with assume(False) will |
| 90 | actually fail, raising: |
| 91 | |
| 92 | ``` |
| 93 | hypothesis.errors.Unsatisfiable: Unable to satisfy assumptions of hypothesis test_stuff. Only 0 examples considered satisfied assumptions |
| 94 | ``` |
| 95 | |
| 96 | This is because of the min\_satisfying\_examples setting: If |
| 97 | Hypothesis couldn't find enough valid test cases then it will |
| 98 | fail the test rather than silently doing the wrong thing. |
| 99 | |
| 100 | min\_satisfying\_examples will never increase the number of tests |
| 101 | run, only fail the test if that number of valid examples haven't |
| 102 | been run. If you're hitting this failure you can either turn it |
| 103 | down or turn the timeout or max\_iterations up. Better, you can |
| 104 | figure out *why* you're hitting that and fix it, because it's |
| 105 | probably a sign you're not getting much benefit out of Hypothesis. |
| 106 | |
| 107 | |
| 108 | ### Failing tests |
| 109 | |
| 110 | If in the course of normal execution Hypothesis finds an example |
| 111 | which causes your test to fail, it switches into shrinking mode. |
| 112 | |
| 113 | Shrinking mode tries to take your example and produce a smaller |
| 114 | one. It ignores max\_examples and max\_iterations but respects |
| 115 | timeout. It also respects one additional setting: max\_shrinks. |
| 116 | |
| 117 | max\_shrinks is the maximum number of *failing* tests that Hypothesis |
| 118 | will see before it stops. It may try any number of valid or invalid |
| 119 | examples in the course of shrinking. This is because failing |
| 120 | examples tend to be a lot rarer than passing or invalid examples, |
| 121 | so it makes more sense to limit based on that if we want to get |
| 122 | good examples out at the end. |
| 123 | |
| 124 | Once Hypothesis has finished shrinking it will run your test once |
| 125 | more to replay it for display: In the final run only it will print |
| 126 | out the example and any notes, and will let the exception bubble |
| 127 | up to the test runner to be handled as normal. |
| 128 | |
| 129 | |
| 130 | ### In Conclusion |
| 131 | |
| 132 | "It's complicated". |
| 133 | |
| 134 | These heuristics are probably not the best. They've evolved |
| 135 | over time, and are definitely not the ones that you or I would |
| 136 | obviously come up with if you sat down and designed the system |
| 137 | from scratch. |
| 138 | |
| 139 | Fortunately, you're not expected to know these heuristics by heart |
| 140 | and mostly shouldn't have to. I'm working on a new feature that |
| 141 | will help show how many examples Hypothesis has tried and help |
| 142 | debug why it's stopped at that point. Hopefully it will be coming |
| 143 | in a release in the near future. |