33
33
34
34
---
35
35
36
- ## Property based testing
36
+ ## Function properties
37
+
38
+
39
+ ----
40
+
41
+ ### Referential transperency
42
+
43
+ * You can replace a function call with the result
44
+ * Caching<br /><!-- .element: class="fragment" data-fragment-index="0" -->
45
+ * Parallelization<br /><!-- .element: class="fragment"-->
46
+ * Pipelining<!-- .element: class="fragment" -->
47
+
48
+ ----
49
+
50
+ ### Pure functions
51
+
52
+ * Functions * rely only on the input* and
53
+ * Deterministic<br /><!-- .element: class="fragment"-->
54
+ * Has no side-effect<br /><!-- .element: class="fragment"-->
55
+ * Works with immutable data structures<br /><!-- .element: class="fragment"-->
56
+ * Simplify reasoning<br /><!-- .element: class="fragment"-->
57
+ * Encourage modularity and compositional<br /><!-- .element: class="fragment"-->
58
+
59
+ ----
60
+
61
+ ### Total Functions
62
+
63
+ * Defined for all possible inputs<br /><!-- .element: class="fragment"-->
64
+ * Should terminate and return a value<!-- .element: class="fragment"-->
65
+
66
+ ``` fsharp
67
+ let div a b = a / b // total?
68
+ ```
69
+ <!-- .element: class="fragment"-->
70
+ ``` fsharp
71
+ let head list = List.head list // total?
72
+ ```
73
+ <!-- .element: class="fragment"-->
74
+
75
+
76
+ ---
77
+
78
+ <!-- .slide: data-background-image="./img/property-based-testing-vs-unit-testing.png" -->
79
+
80
+ ----
37
81
38
82
* Example [ FizzBuzz kata] ( https://codingdojo.org/kata/FizzBuzz/ )
39
83
* Problem statement
@@ -48,9 +92,10 @@ print 'FizzBuzz'.
48
92
49
93
----
50
94
51
- ### Covering with test cases
95
+ ### Example based testing
52
96
53
97
``` fsharp
98
+ [<Fact>]
54
99
let ``Three should be a Fizz`` () =
55
100
test <@ FizzBuzz.fizzBuzz 3 = "Fizz" @>
56
101
@@ -97,33 +142,37 @@ let expectedList =
97
142
| _ -> string i)
98
143
```
99
144
100
- * implementing the algorithm in the test code
101
- * so no worky :(
145
+ * implementing the algorithm in the test code<!-- .element: class="fragment" -->
146
+ * so not really an option :(
102
147
103
148
----
104
149
105
150
### Testing properties instead
106
151
107
152
So the properties of FizzBuzz is:
108
153
109
- 1 . multiples of both three and five print 'FizzBuzz'
110
- 2 . multiples of three print 'Fizz'
111
- 3 . multiples of five print 'Buzz'
112
- 4 . otherwise prints the numbers
154
+ 1 . 1 . multiples of both three and five print 'FizzBuzz'< br /> <!-- .element: class="fragment" -->
155
+ 2 . 2 . multiples of three print 'Fizz'< br /> <!-- .element: class="fragment" -->
156
+ 3 . 3 . multiples of five print 'Buzz'< br /> <!-- .element: class="fragment" -->
157
+ 4 . 4 . otherwise prints the numbers< br /> <!-- .element: class="fragment" -->
113
158
114
159
----
115
160
116
161
### Testing properties
117
162
118
- We then need
163
+ * So we need a way to genrate
164
+ * numbers that are multiply of 3 and 5<br /><!-- .element: class="fragment"-->
165
+ * numbers that are multiply of 3 but not 5<br /><!-- .element: class="fragment"-->
166
+ * numbers that are multiply of 5 but not 3<br /><!-- .element: class="fragment"-->
167
+ * Numbers that are not a multiply of 3 or 5<br /><!-- .element: class="fragment"-->
119
168
120
- * A way to genrate
121
- * numbers that are multiply of 3 and 5
122
- * numbers that are multiply of 3 but not 5
123
- * numbers that are multiply of 5 but not 3
124
- * Numbers that are not a multiply of 3 or 5
169
+ ----
170
+
171
+ ### Custom types
125
172
126
173
``` fsharp
174
+ type MultipleOfOnly3 =
175
+ [<Property>]
127
176
let ```test multiply of three``` (x: MultiplyOfOnly3) =
128
177
test <@ FizzBuzz.fizzBuzz x = "Fizz" @>
129
178
```
@@ -191,7 +240,7 @@ let ``Reverse of reverse of a list is the
191
240
List.rev(List.rev xs) = xs
192
241
```
193
242
194
- Would you? <!-- .element: class="fragment" data-fragment-index="1" -->
243
+ Would you?< br /> <!-- .element: class="fragment" data-fragment-index="1" -->
195
244
196
245
Output: <!-- .element: class="fragment" data-fragment-index="2" -->
197
246
``` nohighlight
@@ -443,7 +492,7 @@ This means
443
492
* You write fewer test cases
444
493
* but get better security
445
494
* PBT forces you to consider what to implement
446
- * PBT forces clean design (as to TDD)
495
+ * PBT forces clean design (as do TDD)
447
496
448
497
note:
449
498
@@ -455,5 +504,6 @@ Design:
455
504
456
505
## References
457
506
507
+ * https://www.the-koi.com/projects/introduction-to-property-based-testing/
458
508
* https://fscheck.github.io/FsCheck/
459
509
* https://fsharpforfunandprofit.com/posts/property-based-testing-2/
0 commit comments