diff --git a/slides/05/img/property-based-testing-vs-unit-testing.png b/slides/05/img/property-based-testing-vs-unit-testing.png new file mode 100644 index 0000000..47ea843 Binary files /dev/null and b/slides/05/img/property-based-testing-vs-unit-testing.png differ diff --git a/slides/05/property-based-testing.md b/slides/05/property-based-testing.md index 288fb87..466d9b7 100644 --- a/slides/05/property-based-testing.md +++ b/slides/05/property-based-testing.md @@ -33,7 +33,51 @@ --- -## Property based testing +## Function properties + + +---- + +### Referential transperency + +* You can replace a function call with the result + * Caching
+ * Parallelization
+ * Pipelining + +---- + +### Pure functions + +* Functions *rely only on the input* and + * Deterministic
+ * Has no side-effect
+ * Works with immutable data structures
+ * Simplify reasoning
+ * Encourage modularity and compositional
+ +---- + +### Total Functions + +* Defined for all possible inputs
+* Should terminate and return a value + +```fsharp +let div a b = a / b // total? +``` + +```fsharp +let head list = List.head list // total? +``` + + + +--- + + + +---- * Example [FizzBuzz kata](https://codingdojo.org/kata/FizzBuzz/) * Problem statement @@ -48,9 +92,10 @@ print 'FizzBuzz'. ---- -### Covering with test cases +### Example based testing ```fsharp +[] let ``Three should be a Fizz`` () = test <@ FizzBuzz.fizzBuzz 3 = "Fizz" @> @@ -97,8 +142,8 @@ let expectedList = | _ -> string i) ``` -* implementing the algorithm in the test code - * so no worky :( +* implementing the algorithm in the test code + * so not really an option:( ---- @@ -106,24 +151,28 @@ let expectedList = So the properties of FizzBuzz is: -1. multiples of both three and five print 'FizzBuzz' -2. multiples of three print 'Fizz' -3. multiples of five print 'Buzz' -4. otherwise prints the numbers +1. 1. multiples of both three and five print 'FizzBuzz'
+2. 2. multiples of three print 'Fizz'
+3. 3. multiples of five print 'Buzz'
+4. 4. otherwise prints the numbers
---- ### Testing properties -We then need +* So we need a way to genrate + * numbers that are multiply of 3 and 5
+ * numbers that are multiply of 3 but not 5
+ * numbers that are multiply of 5 but not 3
+ * Numbers that are not a multiply of 3 or 5
-* A way to genrate - * numbers that are multiply of 3 and 5 - * numbers that are multiply of 3 but not 5 - * numbers that are multiply of 5 but not 3 - * Numbers that are not a multiply of 3 or 5 +---- + +### Custom types ```fsharp +type MultipleOfOnly3 = +[] let ```test multiply of three``` (x: MultiplyOfOnly3) = test <@ FizzBuzz.fizzBuzz x = "Fizz" @> ``` @@ -191,7 +240,7 @@ let ``Reverse of reverse of a list is the List.rev(List.rev xs) = xs ``` -Would you? +Would you?
Output: ```nohighlight @@ -443,7 +492,7 @@ This means * You write fewer test cases * but get better security * PBT forces you to consider what to implement -* PBT forces clean design (as to TDD) +* PBT forces clean design (as do TDD) note: @@ -455,5 +504,6 @@ Design: ## References +* https://www.the-koi.com/projects/introduction-to-property-based-testing/ * https://fscheck.github.io/FsCheck/ * https://fsharpforfunandprofit.com/posts/property-based-testing-2/ \ No newline at end of file