| 1 | --- |
| 2 | tags: technical, details, python, alternatives |
| 3 | date: 2016-12-05 10:00 |
| 4 | title: Integrated vs type based shrinking |
| 5 | author: drmaciver |
| 6 | --- |
| 7 | |
| 8 | One of the big differences between Hypothesis and Haskell QuickCheck is |
| 9 | how shrinking is handled. |
| 10 | |
| 11 | Specifically, the way shrinking is handled in Haskell QuickCheck is bad |
| 12 | and the way it works in Hypothesis (and also in test.check and EQC) is |
| 13 | good. If you're implementing a property based testing system, you should |
| 14 | use the good way. If you're using a property based testing system and it |
| 15 | doesn't use the good way, you need to know about this failure mode. |
| 16 | |
| 17 | Unfortunately many (and possibly most) implementations of property based |
| 18 | testing are based on Haskell's QuickCheck and so make the same mistake. |
| 19 | |
| 20 | <!--more--> |
| 21 | |
| 22 | The big difference is whether shrinking is integrated into generation. |
| 23 | |
| 24 | In Haskell's QuickCheck, shrinking is defined based on *types*: Any |
| 25 | value of a given type shrinks the same way, regardless of how it is |
| 26 | generated. In Hypothesis, test.check, etc. instead shrinking is part |
| 27 | of the generation, and the generator controls how the values it produces |
| 28 | shrinks (this works differently in Hypothesis and test.check, and probably |
| 29 | differently again in EQC, but the user visible result is largely the |
| 30 | same) |
| 31 | |
| 32 | This is not a trivial distinction. Integrating shrinking into generation |
| 33 | has two large benefits: |
| 34 | |
| 35 | * Shrinking composes nicely, and you can shrink anything you can generate |
| 36 | regardless of whether there is a defined shrinker for the type produced. |
| 37 | * You can guarantee that shrinking satisfies the same invariants as generation. |
| 38 | |
| 39 | The first is mostly important from a convenience point of view: Although |
| 40 | there are some things it let you do that you can't do in the type based |
| 41 | approach, they're mostly of secondary importance. It largely just saves |
| 42 | you from the effort of having to write your own shrinkers. |
| 43 | |
| 44 | But the second is *really* important, because the lack of it makes your |
| 45 | test failures potentially extremely confusing. |
| 46 | |
| 47 | To see this, lets consider the following example in Hypothesis: |
| 48 | |
| 49 | ```python |
| 50 | from hypothesis import given |
| 51 | from hypothesis.strategies import integers |
| 52 | |
| 53 | even_numbers = integers().map(lambda x: x * 2) |
| 54 | |
| 55 | |
| 56 | @given(even_numbers) |
| 57 | def test_even_numbers_are_even(n): |
| 58 | assert n % 2 == 0 |
| 59 | ``` |
| 60 | |
| 61 | This test always passes: We generate an even number by multiplying |
| 62 | an integer we've generated by two. No problem. |
| 63 | |
| 64 | Now suppose we made the test fail: |
| 65 | |
| 66 | |
| 67 | ```python |
| 68 | from hypothesis import given |
| 69 | from hypothesis.strategies import integers |
| 70 | |
| 71 | even_numbers = integers().map(lambda x: x * 2) |
| 72 | |
| 73 | |
| 74 | @given(even_numbers) |
| 75 | def test_even_numbers_are_even(n): |
| 76 | assert n % 2 == 0 |
| 77 | assert n <= 4 |
| 78 | ``` |
| 79 | |
| 80 | This test will of course fail: Any value of n which is at least 5 will |
| 81 | fail the test. |
| 82 | |
| 83 | But which assertion will fail, and what value will cause it to fail? |
| 84 | |
| 85 | In Hypothesis it's the second one, and it will fail with n=6: The numbers |
| 86 | passed in will still always be even integers, and the smallest even |
| 87 | integer which fails the test is 6. |
| 88 | |
| 89 | But suppose Hypothesis implemented type based shrinking (very early |
| 90 | versions of it *did* implement type based shrinking, but this stopped |
| 91 | being the case somewhere well before 1.0 when the API looked very |
| 92 | different). |
| 93 | |
| 94 | In that case Hypothesis would just know that these things that were |
| 95 | failing the tests were integers, so it would say "How about 1? 1 is a |
| 96 | perfectly valid integer. Lets try 1". |
| 97 | |
| 98 | It would pass in n=1, the first assertion would trigger, and the test |
| 99 | would promptly fail. A successful shrink! |
| 100 | |
| 101 | But it's completely not what we wanted. We were just trying to test on |
| 102 | even integers and the shrinking messed this up. |
| 103 | |
| 104 | This is in some sense the classic |
| 105 | [shrinkers are fuzzers](http://blog.regehr.org/archives/1284) problem |
| 106 | where an error is reduced to a different error, but it's a particularly |
| 107 | annoying version of that because an error we care about is being reduced |
| 108 | to an error we don't care about. |
| 109 | |
| 110 | So we have to duplicate |
| 111 | the constraint logic in our test to make this work: |
| 112 | |
| 113 | |
| 114 | ```python |
| 115 | from hypothesis import assume, given |
| 116 | from hypothesis.strategies import integers |
| 117 | |
| 118 | even_numbers = integers().map(lambda x: x * 2) |
| 119 | |
| 120 | |
| 121 | @given(even_numbers) |
| 122 | def test_even_numbers_are_even(n): |
| 123 | assume(n % 2 == 0) |
| 124 | assert n % 2 == 0 |
| 125 | assert n <= 4 |
| 126 | ``` |
| 127 | |
| 128 | (Having both the assume and the first assert there is of course |
| 129 | redundant) |
| 130 | |
| 131 | In this example the problem was relatively obvious and so easy to |
| 132 | work around, but as your invariants get more implicit and subtle |
| 133 | it becomes really problematic: In Hypothesis it's easy and |
| 134 | convenient to |
| 135 | [generate quite complex data](../generating-the-right-data/), |
| 136 | and trying to recreate the invariants that are automatically |
| 137 | satisfied with that in your tests and/or your custom shrinkers would |
| 138 | quickly become a nightmare. |
| 139 | |
| 140 | I don't think it's an accident that the main systems to get this right are |
| 141 | in dynamic languages. It's certainly not *essential* - [the original proposal that |
| 142 | led to the implementation for test.check was for |
| 143 | Haskell](https://mail.haskell.org/pipermail/libraries/2013-November/021674.html), |
| 144 | and [Jack](https://github.com/ambiata/disorder.hs/tree/master/disorder-jack) is |
| 145 | an alternative property based system for Haskell that does this - but you |
| 146 | feel the pain much more quickly in dynamic languages because the typical |
| 147 | workaround for this problem in Haskell is to define a newtype, which lets you |
| 148 | turn off the default shrinking for your types and possibly define your own. |
| 149 | |
| 150 | But that's a workaround for a problem that shouldn't be there in the first place, |
| 151 | and using it will still result in your having to encode the invariants into your |
| 152 | your shrinkers, which is more work and more brittle than just having it work |
| 153 | automatically. |
| 154 | |
| 155 | So although (as far as I know) none of the currently popular property based |
| 156 | testing systems for statically typed languages implement this behaviour |
| 157 | correctly, they absolutely can and they absolutely should. It will improve |
| 158 | users' lives significantly. |
| 159 | |
| 160 | This of course goes doubly for dynamic languages, where even working around |
| 161 | this problem is hard. |
| 162 | |
| 163 | So, in conclusion, just say no to type based shrinking. It's a bad idea, |
| 164 | and failures your property based tests will become significantly harder |
| 165 | to understand in its presence. |