Debian Codegraph @

website/content/2016-05-13-what-is-property-based-testing.md

raw
1 ---
2 tags: non-technical, philosophy
3 date: 2016-05-14 09:00
4 title: What is Property Based Testing?
5 author: drmaciver
6 ---
7
8 I get asked this a lot, and I write property based testing tools for a living, so you'd think
9 I have a good answer to this, but historically I haven't. This is my attempt to fix that.
10
11 Historically the definition of property based testing has been "The thing that
12 [QuickCheck](../quickcheck-in-every-language/) does". As
13 a working definition this has served pretty well, but the problem is that it makes us unable
14 to distinguish what the essential features of property-based testing are and what are just
15 [accidental features that appeared in the implementations that we're used to](../referential-transparency/).
16
17 As the author of a property based testing system which diverges quite a lot from QuickCheck,
18 this troubles me more than it would most people, so I thought I'd set out some of my thoughts on
19 what property based testing is and isn't.
20
21 This isn't intended to be definitive, and it will evolve over time as my thoughts do, but
22 it should provide a useful grounding for further discussion.
23
24 <!--more-->
25
26 There are essentially two ways we can draw a boundary for something like this: We can go
27 narrow or we can go wide. i.e. we can restrict our definitions to things that look exactly
28 like QuickCheck, or things that are in the same general family of behaviour. My inclination
29 is always to go wide, but I'm going to try to rein that in for the purpose of this piece.
30
31 But I'm still going to start by planting a flag. The following are *not* essential features
32 of property based testing:
33
34 1. [Referential Transparency](../referential-transparency/).
35 2. Types
36 3. Randomization
37 4. The use of any particular tool or library
38
39 As evidence I present the following:
40
41 1. Almost every property based testing library, including but not limited to Hypothesis and
42 QuickCheck (both Erlang and Haskell).
43 2. The many successful property based testing systems for dynamic languages. e.g. Erlang
44 QuickCheck, test.check, Hypothesis.
45 3. [SmallCheck](https://hackage.haskell.org/package/smallcheck). I have mixed feelings about
46 its effectiveness, but it's unambiguously property-based testing.
47 4. It's very easy to hand-roll your own testing protocols for property-based testing of a
48 particular result. For example, I've [previously done this for testing a code formatter](
49 http://www.drmaciver.com/2015/03/27-bugs-in-24-hours/): Run it over a corpus (more on
50 whether running over a corpus "really" counts in a second) of Python files, check whether
51 the resulting formatted code satisfies PEP8. It's classic property-based testing with an
52 oracle.
53
54 So that provides us with a useful starting point of things that are definitely property based
55 testing. But you're never going to find a good definition by looking at only positive examples,
56 so let's look at some cases where it's more arguable.
57
58 First off, lets revisit that parenthetical question: Does just testing against a large corpus count?
59
60 I'm going to go with "probably". I think if we're counting SmallCheck we need to count testing
61 against a large corpus: If you take the first 20k outputs that would be generated by SmallCheck
62 and just replay the test using those the first N of those each time, you're doing exactly the
63 same sort of testing. Similarly if you draw 20k outputs using Hypothesis and then just randomly
64 sample from them each time.
65
66 I think drawing from a small, fixed, corpus probably *doesn't* count. If you could feasibly
67 write a property based test as 10 example based tests in line in your source code, it's
68 probably really just example based testing. This boundary is a bit, um, fuzzy though.
69
70 On which note, what about fuzzing?
71
72 I have previously argued that fuzzing is just a form of property-based testing - you're testing
73 the property "it doesn't crash". I think I've reversed my opinion on this. In particular, I think
74 [the style of testing I advocate for getting started with Hypothesis](../getting-started-with-hypothesis/), *probably* doesn't
75 count as property based testing.
76
77 I'm unsure about this boundary. The main reason I'm drawing it here is that they do feel like
78 they have a different character - property based testing requires you to reason about how your
79 program should behave, while fuzzing can just be applied to arbitrary programs with minimal
80 understanding of their behaviour - and also that fuzzing feels somehow more fundamental.
81
82 But you can certainly do property based testing using fuzzing tools, in the same way that you
83 can do it with hand-rolled property based testing systems - I could have taken my Python formatting
84 test above, added [python-afl](http://jwilk.net/software/python-afl) to the mix, and
85 that would still be property based testing.
86
87 Conversely, you can do fuzzing with property-based testing tools: If fuzzing is not property
88 based testing then not all tests using Hypothesis, QuickCheck, etc. are property based tests.
89 I'm actually OK with that. There's a long tradition of testing tools being used outside their
90 domain - e.g. most test frameworks originally designed as unit testing tools end up getting
91 being used for the whole gamut of testing.
92
93 So with that in mind, lets provide a definition of fuzzing that I'd like to use:
94
95 > Fuzzing is feeding a piece of code (function, program, etc.) data from a large corpus, possibly
96 > dynamically generated, possibly dependent on the results of execution on previous data, in
97 > order to see whether it fails.
98
99 The definitions of "data" and "whether it fails" will vary from fuzzer to fuzzer - some fuzzers
100 will generate only binary data, some fuzzers will generate more structured data. Some fuzzers
101 will look for a process crash, some might just look for a function returning false.
102
103 (Often definitions of fuzzing focus on "malformed" data. I think this is misguided and fails
104 to consider a lot of things that people would obviously consider fuzzers. e.g. [CSmith](https://embed.cs.utah.edu/csmith/)
105 is certainly a type of fuzzer but deliberately only generates well formed C programs).
106
107 And given that definition, I think I can now provide a definition of property-based testing:
108
109 > Property based testing is the construction of tests such that, when these tests are fuzzed,
110 > failures in the test reveal problems with the system under test that could not have been
111 > revealed by direct fuzzing of that system.
112
113 (If you feel strongly that fuzzing *should* count as property-based testing you can just drop
114 the 'that could not have been etc.' part. I'm on the fence about it myself.)
115
116 These extra modes of failure then constitute the properties that we're testing.
117
118 I think this works pretty well at capturing what we're doing with property based testing. It's
119 not perfect, but I'm pretty happy with it. One of the things I particularly like is that it makes
120 it clear that property-based testing is what *you* do, not what the computer does. The part
121 that the computer does is "just fuzzing".
122
123 Under this point of view, a property-based testing library is really two parts:
124
125 1. A fuzzer.
126 2. A library of tools for making it easy to construct property-based tests using that fuzzer.
127
128 Hypothesis is very explicitly designed along these lines - the core of it is a structured
129 fuzzing library called Conjecture - which suggests that I may have a bit of bias here, but
130 I still feel that it captures the behaviour of most other property based testing systems
131 quite well, and provides quite a nice middle ground between the wider definition that I
132 wanted and the more tightly focused definition that QuickCheck orients people around.