| 1 | --- |
| 2 | tags: technical, python, properties, intro |
| 3 | date: 2016-06-30 00:00 |
| 4 | title: Testing as a Complete Specification |
| 5 | author: drmaciver |
| 6 | --- |
| 7 | |
| 8 | Sometimes you're lucky enough to have problems where the result is |
| 9 | completely specified by a few simple properties. |
| 10 | |
| 11 | This doesn't necessarily correspond to them being easy! Many such |
| 12 | problems are actually extremely fiddly to implement. |
| 13 | |
| 14 | It does mean that they're easy to *test* though. Lets see how. |
| 15 | |
| 16 | <!--more--> |
| 17 | |
| 18 | Let's look at the problem of doing a binary search. Specifically we'll |
| 19 | look at a left biased binary search: Given a sorted list and some value, |
| 20 | we want to find the smallest index that we can insert that value at and |
| 21 | still have the result be sorted. |
| 22 | |
| 23 | So we've got the following properties: |
| 24 | |
| 25 | 1. binary_search must always return a valid index to insert the value |
| 26 | at. |
| 27 | 2. If we insert the value at that index the result must be sorted. |
| 28 | 3. If we insert the value at any *smaller* index, the result must *not* |
| 29 | be sorted. |
| 30 | |
| 31 | Using Hypothesis we can write down tests for all these properties: |
| 32 | |
| 33 | ```python |
| 34 | from hypothesis import given, strategies as st |
| 35 | |
| 36 | |
| 37 | @given(lists(integers()).map(sorted), integers()) |
| 38 | def test_binary_search_gives_valid_index(ls, v): |
| 39 | i = binary_search(ls, v) |
| 40 | assert 0 <= i <= len(ls) |
| 41 | |
| 42 | |
| 43 | @given(lists(integers()).map(sorted), integers()) |
| 44 | def test_inserting_at_binary_search_remains_sorted(ls, v): |
| 45 | i = binary_search(ls, v) |
| 46 | ls.insert(i, v) |
| 47 | assert sorted(ls) == ls |
| 48 | |
| 49 | |
| 50 | @given(lists(integers()).map(sorted), integers()) |
| 51 | def test_inserting_at_smaller_index_gives_unsorted(ls, v): |
| 52 | for i in range(binary_search(ls, v)): |
| 53 | ls2 = list(ls) |
| 54 | ls2.insert(i, v) |
| 55 | assert sorted(ls2) != ls |
| 56 | ``` |
| 57 | |
| 58 | If these tests pass, our implementation must be perfectly correct, |
| 59 | right? They capture the specification of the binary_search function |
| 60 | exactly, so they should be enough. |
| 61 | |
| 62 | And they mostly are, but they suffer from one problem that will |
| 63 | sometimes crop up with property-based testing: They don't hit all bugs |
| 64 | with quite high enough probability. |
| 65 | |
| 66 | This is the difference between testing and mathematical proof: A proof |
| 67 | will guarantee that these properties *always* hold, while a test can |
| 68 | only guarantee that they hold in the areas that it's checked. A test |
| 69 | using Hypothesis will check a much wider area than most hand-written |
| 70 | tests, but it's still limited to a finite set of examples. |
| 71 | |
| 72 | Lets see how this can cause us problems. Consider the following |
| 73 | implementation of binary search: |
| 74 | |
| 75 | ```python |
| 76 | def binary_search(list, value): |
| 77 | if not list: |
| 78 | return 0 |
| 79 | if value > list[-1]: |
| 80 | return len(list) |
| 81 | if value <= list[0]: |
| 82 | return 0 |
| 83 | lo = 0 |
| 84 | hi = len(list) - 1 |
| 85 | while lo + 1 < hi: |
| 86 | mid = (lo + hi) // 2 |
| 87 | pivot = list[mid] |
| 88 | if value < pivot: |
| 89 | hi = mid |
| 90 | elif value == pivot: |
| 91 | return mid |
| 92 | else: |
| 93 | lo = mid |
| 94 | return hi |
| 95 | ``` |
| 96 | |
| 97 | This implements the common check that if our pivot index ever has |
| 98 | exactly the right value we return early there. Unfortunately in this |
| 99 | case that check is wrong: It violates the property that we should |
| 100 | always find the *smallest* property, so the third test should fail. |
| 101 | |
| 102 | And sure enough, if you run the test enough times it eventually *does* |
| 103 | fail: |
| 104 | |
| 105 | ``` |
| 106 | Falsifying example: test_inserting_at_smaller_index_gives_unsorted( |
| 107 | ls=[0, 1, 1, 1, 1], v=1 |
| 108 | ) |
| 109 | ``` |
| 110 | |
| 111 | (you may also get (ls=[-1, 0, 0, 0, 0], v=0)) |
| 112 | |
| 113 | However when I run it it usually *doesn't* fail the first time. It |
| 114 | usually takes somewhere between two and five runs before it fails. This |
| 115 | is because in order to trigger this behaviour being wrong you need |
| 116 | quite specific behaviour: value needs to appear in ls at least |
| 117 | twice, and it needs to do so in such a way that one of the indices where |
| 118 | it appears that is *not* the first one gets chosen as mid at some |
| 119 | point in the process. Hypothesis does some things that boost the |
| 120 | chances of this happening, but they don't boost it *that* much. |
| 121 | |
| 122 | Of course, once it starts failing Hypothesis's test database kicks in, |
| 123 | and the test keeps failing until the bug is fixed, but low probability |
| 124 | failures are still annoying because they move the point at which you |
| 125 | discover the problem further away from when you introduced it. This is |
| 126 | especially true when you're using [stateful testing |
| 127 | ](../rule-based-stateful-testing/), |
| 128 | because the search space is so large that there are a lot of low |
| 129 | probability bugs. |
| 130 | |
| 131 | Fortunately there's an easy fix for this case: You can write additional |
| 132 | tests that are more likely to discover bugs because they are less |
| 133 | sensitively dependent on the example chosen by Hypothesis to exhibit |
| 134 | interesting behaviours. |
| 135 | |
| 136 | Consider the following test: |
| 137 | |
| 138 | ```python |
| 139 | @given(lists(integers()).map(sorted), integers()) |
| 140 | def test_inserting_at_result_point_and_searching_again(ls, v): |
| 141 | i = binary_search(ls, v) |
| 142 | ls.insert(i, v) |
| 143 | assert binary_search(ls, v) == i |
| 144 | ``` |
| 145 | |
| 146 | The idea here is that by doing a search, inserting the value at that |
| 147 | index, and searching again we cannot have moved the insert point: |
| 148 | Inserting there again would still result in a sorted list, and inserting |
| 149 | any earlier would still have resulted in an unsorted list, so this must |
| 150 | still be the same insert point (this should remind you a bit of |
| 151 | [the approach for testing optimizers we used before]( |
| 152 | ../testing-optimizers-with-hypothesis/) |
| 153 | ). |
| 154 | |
| 155 | This test fails pretty consistently because it doesn't rely nearly so |
| 156 | much on finding duplicates: Instead it deliberately creates them in a |
| 157 | place where they are likely to be problematic. |
| 158 | |
| 159 | So, in conclusion: |
| 160 | |
| 161 | 1. When the problem is fully specified, this gives you a natural source |
| 162 | of tests that you can easily write using Hypothesis. |
| 163 | 2. However this is where your tests should *start* rather than finish, |
| 164 | and you still need to think about other interesting ways to test your |
| 165 | software. |