| 1 | --- |
| 2 | tags: python, details, technical |
| 3 | date: 2016-04-16 06:00 |
| 4 | title: Anatomy of a Hypothesis Based Test |
| 5 | author: drmaciver |
| 6 | --- |
| 7 | |
| 8 | What happens when you run a test using Hypothesis? This article will help you understand. |
| 9 | |
| 10 | <!--more--> |
| 11 | |
| 12 | The Python version of Hypothesis uses *decorators* to transform a test function that |
| 13 | doesn't use Hypothesis into one that does. |
| 14 | |
| 15 | Consider the following example using [py.test](http://pytest.org/latest/) style testing: |
| 16 | |
| 17 | ```python |
| 18 | from hypothesis import given |
| 19 | from hypothesis.strategies import floats |
| 20 | |
| 21 | |
| 22 | @given(floats(), floats()) |
| 23 | def test_floats_are_commutative(x, y): |
| 24 | assert x + y == y + x |
| 25 | ``` |
| 26 | |
| 27 | The inner function takes two arguments, but the wrapping function defined by the @given |
| 28 | decorator takes none and may be invoked as a normal test: |
| 29 | |
| 30 | ```bash |
| 31 | python -m pytest test_floats.py |
| 32 | ``` |
| 33 | |
| 34 | And we see the following output from py.test: |
| 35 | |
| 36 | ``` |
| 37 | @given(floats(), floats()) |
| 38 | def test_floats_are_commutative(x, y): |
| 39 | > assert x + y == y + x |
| 40 | E assert (0.0 + nan) == (nan + 0.0) |
| 41 | |
| 42 | test_floats.py:7: AssertionError |
| 43 | |
| 44 | Falsifying example: test_floats_are_commutative(x=0.0, y=nan) |
| 45 | ``` |
| 46 | |
| 47 | The test fails, because [nan](https://en.wikipedia.org/wiki/NaN) is a valid floating |
| 48 | point number which is not equal to itself, and adding anything to nan yields nan. |
| 49 | |
| 50 | When we ran this, Hypothesis invoked our test function with a number of randomly chosen |
| 51 | values for the arguments until it found one that failed. It then attempted to *shrink* |
| 52 | those values to produce a simpler one that would also fail. |
| 53 | |
| 54 | If we wanted to see what it actually called our function with we can set the *verbosity |
| 55 | level*. This can either be done in code with settings, or by specifying an environment |
| 56 | variable: |
| 57 | |
| 58 | |
| 59 | ```python |
| 60 | from hypothesis import Verbosity, given, settings |
| 61 | from hypothesis.strategies import floats |
| 62 | |
| 63 | |
| 64 | @settings(verbosity=Verbosity.verbose) |
| 65 | @given(floats(), floats()) |
| 66 | def test_floats_are_commutative(x, y): |
| 67 | assert x + y == y + x |
| 68 | ``` |
| 69 | |
| 70 | ```bash |
| 71 | HYPOTHESIS_VERBOSITY_LEVEL=verbose python -m pytest test_floats.py |
| 72 | ``` |
| 73 | |
| 74 | Any verbosity values explicitly passed in settings will override whatever is set at |
| 75 | the environment level - the latter just provides a default. |
| 76 | |
| 77 | Whichever one we choose, running it we'll see output something like the following: |
| 78 | |
| 79 | ``` |
| 80 | |
| 81 | Trying example: test_floats_are_commutative(x=-0.05851890381391768, y=-6.060045836901702e+300) |
| 82 | Trying example: test_floats_are_commutative(x=-0.06323690311413645, y=2.0324087421708266e-308) |
| 83 | Trying example: test_floats_are_commutative(x=-0.05738038380011458, y=-1.5993500302384265e-308) |
| 84 | Trying example: test_floats_are_commutative(x=-0.06598754758697359, y=-1.1412902232349034e-308) |
| 85 | Trying example: test_floats_are_commutative(x=-0.06472919559855002, y=1.7429441378277974e+35) |
| 86 | Trying example: test_floats_are_commutative(x=-0.06537123121982172, y=-8.136220566134233e-156) |
| 87 | Trying example: test_floats_are_commutative(x=-0.06016703321602157, y=1.9718842567475311e-215) |
| 88 | Trying example: test_floats_are_commutative(x=-0.055257588875432875, y=1.578407827448836e-308) |
| 89 | Trying example: test_floats_are_commutative(x=-0.06313031758042666, y=1.6749023021600297e-175) |
| 90 | Trying example: test_floats_are_commutative(x=-0.05886897920547916, y=1.213699633272585e+292) |
| 91 | Trying example: test_floats_are_commutative(x=-12.0, y=-0.0) |
| 92 | Trying example: test_floats_are_commutative(x=4.0, y=1.7976931348623157e+308) |
| 93 | Trying example: test_floats_are_commutative(x=-9.0, y=0.0) |
| 94 | Trying example: test_floats_are_commutative(x=-38.0, y=1.7976931348623157e+308) |
| 95 | Trying example: test_floats_are_commutative(x=-24.0, y=1.5686642754811104e+289) |
| 96 | Trying example: test_floats_are_commutative(x=-10.0, y=nan) |
| 97 | Traceback (most recent call last): |
| 98 | ... |
| 99 | AssertionError: assert (-10.0 + nan) == (nan + -10.0) |
| 100 | |
| 101 | Trying example: test_floats_are_commutative(x=10.0, y=nan) |
| 102 | Traceback (most recent call last): |
| 103 | ... |
| 104 | AssertionError: assert (10.0 + nan) == (nan + 10.0) |
| 105 | |
| 106 | Trying example: test_floats_are_commutative(x=0.0, y=nan) |
| 107 | Traceback (most recent call last): |
| 108 | ... |
| 109 | AssertionError: assert (0.0 + nan) == (nan + 0.0) |
| 110 | |
| 111 | Trying example: test_floats_are_commutative(x=0.0, y=0.0) |
| 112 | Trying example: test_floats_are_commutative(x=0.0, y=inf) |
| 113 | Trying example: test_floats_are_commutative(x=0.0, y=-inf) |
| 114 | Falsifying example: test_floats_are_commutative(x=0.0, y=nan) |
| 115 | ``` |
| 116 | |
| 117 | Notice how the first failing example we got was -10.0, nan but Hypothesis was able |
| 118 | to turn that into 0.0, nan. That's the shrinking at work. For a simple case like this it |
| 119 | doesn't matter so much, but as your examples get complicated it's essential for making |
| 120 | Hypothesis's output easy to understand. |
| 121 | |
| 122 | |
| 123 | ``` |
| 124 | Trying example: test_floats_are_commutative(x=nan, y=0.0) |
| 125 | Traceback (most recent call last): |
| 126 | ... |
| 127 | AssertionError: assert (nan + 0.0) == (0.0 + nan) |
| 128 | |
| 129 | Trying example: test_floats_are_commutative(x=0.0, y=0.0) |
| 130 | Trying example: test_floats_are_commutative(x=inf, y=0.0) |
| 131 | Trying example: test_floats_are_commutative(x=-inf, y=0.0) |
| 132 | Falsifying example: test_floats_are_commutative(x=nan, y=0.0) |
| 133 | ``` |
| 134 | |
| 135 | Now lets see what happens when we rerun the test: |
| 136 | |
| 137 | |
| 138 | ``` |
| 139 | Trying example: test_floats_are_commutative(x=0.0, y=nan) |
| 140 | Traceback (most recent call last): |
| 141 | ... |
| 142 | AssertionError: assert (0.0 + nan) == (nan + 0.0) |
| 143 | |
| 144 | Trying example: test_floats_are_commutative(x=0.0, y=0.0) |
| 145 | Trying example: test_floats_are_commutative(x=0.0, y=inf) |
| 146 | Trying example: test_floats_are_commutative(x=0.0, y=-inf) |
| 147 | Falsifying example: test_floats_are_commutative(x=0.0, y=nan) |
| 148 | ``` |
| 149 | |
| 150 | Notice how the first example it tried was the failing example we had last time? That's |
| 151 | not an accident. Hypothesis has an example database where it saves failing examples. |
| 152 | When it starts up it looks for any examples it has seen failing previously and tries |
| 153 | them first before any random generation occurs. If any of them fail, we take that |
| 154 | failure as our starting point and move straight to the shrinking phase without any |
| 155 | generation. |
| 156 | |
| 157 | The database format is safe to check in to version control if you like and will merge |
| 158 | changes correctly out of the box, but it's often clearer to specify the examples you |
| 159 | want to run every time in the source code as follows: |
| 160 | |
| 161 | |
| 162 | ```python |
| 163 | from hypothesis import example, given |
| 164 | from hypothesis.strategies import floats |
| 165 | |
| 166 | |
| 167 | @example(0.0, float("nan")) |
| 168 | @given(floats(), floats()) |
| 169 | def test_floats_are_commutative(x, y): |
| 170 | assert x + y == y + x |
| 171 | ``` |
| 172 | |
| 173 | ``` |
| 174 | Falsifying example: test_floats_are_commutative(x=0.0, y=nan) |
| 175 | ``` |
| 176 | |
| 177 | If you run this in verbose mode it will print out |
| 178 | Falsifying example: test\_floats\_are\_commutative(x=0.0, y=nan) immediately and |
| 179 | not try to do any shrinks. Values you pass in via example will not be shrunk. |
| 180 | This is partly a technical limitation but it can often be useful as well. |
| 181 | |
| 182 | Explicitly provided examples are run before any generated examples. |
| 183 | |
| 184 | So, to recap and elaborate, when you use a test written using Hypothesis: |
| 185 | |
| 186 | 1. Your test runner sees the decorated test as if it were a perfectly normal test function |
| 187 | and invokes it. |
| 188 | 2. Hypothesis calls your test function with each explicitly provided @example. If one |
| 189 | of these fails it stops immediately and bubbles up the exception for the test runner to handle. |
| 190 | 3. Hypothesis reads examples out of its database of previously failing examples. If any of them |
| 191 | fail, it stops there and proceeds to the shrinking step with that example. Otherwise it continues |
| 192 | to the generation step. |
| 193 | 4. Hypothesis tries generating a number of examples. If any of these raises an exception, it stops |
| 194 | there and proceeds to the shrinking step. If none of them raise an exception, it silently returns |
| 195 | and the test passes. |
| 196 | 5. Hypothesis takes the previously failing example it's seen and tries to produce a "Simpler" version |
| 197 | of it. Once it has found the simplest it possibly can, it saves that in the example database (in |
| 198 | actual fact it saves every failing example in the example database as it shrinks, but the reasons |
| 199 | why aren't important right now). |
| 200 | 6. Hypothesis takes the simplest failing example and replays it, finally letting the test bubble up to |
| 201 | the test runner. |