Skip to content

Commit 7a67c70

Browse files
author
Henrik Kirk
committed
Updates to pbt slides
1 parent 9d09dda commit 7a67c70

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed
Loading

slides/05/property-based-testing.md

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,51 @@
3333

3434
---
3535

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+
----
3781

3882
* Example [FizzBuzz kata](https://codingdojo.org/kata/FizzBuzz/)
3983
* Problem statement
@@ -48,9 +92,10 @@ print 'FizzBuzz'.
4892

4993
----
5094

51-
### Covering with test cases
95+
### Example based testing
5296

5397
```fsharp
98+
[<Fact>]
5499
let ``Three should be a Fizz`` () =
55100
test <@ FizzBuzz.fizzBuzz 3 = "Fizz" @>
56101
@@ -97,33 +142,37 @@ let expectedList =
97142
| _ -> string i)
98143
```
99144

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:(
102147

103148
----
104149

105150
### Testing properties instead
106151

107152
So the properties of FizzBuzz is:
108153

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"-->
113158

114159
----
115160

116161
### Testing properties
117162

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"-->
119168

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
125172

126173
```fsharp
174+
type MultipleOfOnly3 =
175+
[<Property>]
127176
let ```test multiply of three``` (x: MultiplyOfOnly3) =
128177
test <@ FizzBuzz.fizzBuzz x = "Fizz" @>
129178
```
@@ -191,7 +240,7 @@ let ``Reverse of reverse of a list is the
191240
List.rev(List.rev xs) = xs
192241
```
193242

194-
Would you? <!-- .element: class="fragment" data-fragment-index="1" -->
243+
Would you?<br/> <!-- .element: class="fragment" data-fragment-index="1" -->
195244

196245
Output: <!-- .element: class="fragment" data-fragment-index="2" -->
197246
```nohighlight
@@ -443,7 +492,7 @@ This means
443492
* You write fewer test cases
444493
* but get better security
445494
* PBT forces you to consider what to implement
446-
* PBT forces clean design (as to TDD)
495+
* PBT forces clean design (as do TDD)
447496

448497
note:
449498

@@ -455,5 +504,6 @@ Design:
455504

456505
## References
457506

507+
* https://www.the-koi.com/projects/introduction-to-property-based-testing/
458508
* https://fscheck.github.io/FsCheck/
459509
* https://fsharpforfunandprofit.com/posts/property-based-testing-2/

0 commit comments

Comments
 (0)