Slide 1

Slide 1 text

Introduction to Property-Based Testing cybai @ COSCUP 2022

Slide 2

Slide 2 text

2 sli.do #coscup22-fp

Slide 3

Slide 3 text

● @CYBAI ● @_cybai ● Front-End Engineer @ Autify, Inc. ● Web Front-End by day, learning Functional Programming by night. whoami 3

Slide 4

Slide 4 text

Agenda 1. What is Property-Based Testing? 2. More examples for Property-Based Testing 3. When to Example-Based Testing? 4. Advanced topics? 5. Q & A 4 Note: PBT refers to Property-Based Testing in this talk.

Slide 5

Slide 5 text

What is Property-Based Testing? 5

Slide 6

Slide 6 text

What is Property-Based Testing? 6

Slide 7

Slide 7 text

What is Property? 7

Slide 8

Slide 8 text

A property is an invariant of your testing target. What is Property? 8

Slide 9

Slide 9 text

What is Property? 9 A property is an invariant of your testing target. What does that mean? Let’s start from the traditional example-based testing.

Slide 10

Slide 10 text

What is Property-Based Testing? (Cont.) 10 Haskell: QuickCheck, Hedgehog Rust: quickcheck, proptest Erlang: PropEr JavaScript: fast-check, jsverify Python: Hypothesis OCaml: popper … and many more PBT frameworks for different languages

Slide 11

Slide 11 text

11

Slide 12

Slide 12 text

The function under test 12

Slide 13

Slide 13 text

The function under test Sample Inputs 13

Slide 14

Slide 14 text

The function under test Expected Outputs 14 Sample Inputs

Slide 15

Slide 15 text

What is Property-Based Testing? (Cont.) 15 E.g. [1,2,3], [0], [], [1,2,3,...,100]

Slide 16

Slide 16 text

What is Property-Based Testing? (Cont.) A test case should be like a proof for the invariant for the testing target. 16

Slide 17

Slide 17 text

A test case should be like a proof for the invariant for the testing target. In the example of `reverse` function, the invariant here is “An input array should be kept as same if it’s reversed twice”. What is Property-Based Testing? (Cont.) 17

Slide 18

Slide 18 text

What is Property-Based Testing? (Cont.) 1. Arbitraries 2. Runners 3. Shrinkers 18

Slide 19

Slide 19 text

What is Property-Based Testing? (Cont.) 1. Arbitraries 2. Runners 3. Shrinkers fc.integer() fc.webUrl() fc.oneof() fc.record() fc.boolean() fc.nat() fc.string() .filter() fc.option() .map() 19 Generate random values

Slide 20

Slide 20 text

What values will fast-check Arbitraries generate? 20 fc.integer() => -1000, -10, …, 0, 1, 3, …, 100, … fc.nat() => 0, 1, 2, …, 100000, …

Slide 21

Slide 21 text

What values will fast-check Arbitraries generate? 21 fc.integer() => -1000, -10, …, 0, 1, 3, …, 100, … fc.boolean() => true, false fc.nat() => 0, 1, 2, …, 100000, … fc.string() => "", "C0SCup", "p#@91-+", …

Slide 22

Slide 22 text

What values will fast-check Arbitraries generate? 22 fc.integer() => -1000, -10, …, 0, 1, 3, …, 100, … fc.oneof(fc.nat(), fc.char()) => Use one of the given arbitraries fc.boolean() => true, false fc.nat() => 0, 1, 2, …, 100000, … fc.string() => "", "C0SCup", "p#@91-+", … .filter() => precondition for the arbitrary (e.g. n < 100) fc.array(fc.nat()) => [], [1], [0,2,2,3], …, [3,5, …, 10,100]

Slide 23

Slide 23 text

What values will fast-check Arbitraries generate? 23 fc.integer() => -1000, -10, …, 0, 1, 3, …, 100, … fc.webUrl() => "https://lo.ui", "https://4.xlm", … fc.oneof(fc.nat(), fc.char()) => Use one of the given arbitraries fc.record({ age: fc.nat(), name: fc.string() }) => Arbitrary for given object model, e.g. { age: 2, name: "John" } fc.boolean() => true, false fc.nat() => 0, 1, 2, …, 100000, … fc.string() => "", "C0SCup", "p#@91-+", … .filter() => precondition for the arbitrary (e.g. n < 100) fc.array(fc.nat()) => [], [1], [0,2,2,3], …, [3,5, …, 10,100] fc.option(fc.nat()) => null, 0, 1, …, 100, …, 100000, …

Slide 24

Slide 24 text

What is Property-Based Testing? (Cont.) 1. Arbitraries 2. Runners 3. Shrinkers Run tests for properties and validate specified predicates. Most of PBT frameworks default to 100 number of runs but you can change it. 24

Slide 25

Slide 25 text

What is Property-Based Testing? (Cont.) 1. Arbitraries 2. Runners 3. Shrinkers Teach the PBT framework how to shrink the failed test case. This would be helpful for knowing smallest possible case. (but not every PBT framework supports this) 25

Slide 26

Slide 26 text

More examples for Property-Based Testing 26

Slide 27

Slide 27 text

More examples - reverse 27

Slide 28

Slide 28 text

More examples - reverse (Cont.) 28

Slide 29

Slide 29 text

More examples - reverse (Cont.) 29

Slide 30

Slide 30 text

More examples - reverse (Cont.) 30 shrunk times and shrunk test case

Slide 31

Slide 31 text

More examples - Calculate Shipping Fee 31

Slide 32

Slide 32 text

More examples - Calculate Shipping Fee (Cont.) 32

Slide 33

Slide 33 text

More examples - Calculate Shipping Fee (Cont.) 33

Slide 34

Slide 34 text

More examples - Calculate Shipping Fee (Cont.) 34

Slide 35

Slide 35 text

More examples for Property-Based Testing You can find more examples on Advent of PBT 2021 created by the author of fast-check. 35

Slide 36

Slide 36 text

When to Example-Based Testing? You can still write unit tests in example-based testing way to make people easier to understand how to use them. I’d highly recommend test them with `doctest`. 36

Slide 37

Slide 37 text

When to Example-Based Testing? Let’s see how `doctest` is utilized in Rust. By default, code block in doc comments can be tested by `cargo test` or `rustdoc --test`. So, it can be tested as a part of document. 37 The calculate shipping fee example in Rust

Slide 38

Slide 38 text

Advanced Topics? 38

Slide 39

Slide 39 text

1. PBT for End-to-End Testing (like this post) Advanced Topics? 39

Slide 40

Slide 40 text

1. PBT for End-to-End Testing (like this post) 2. Model-based testing (like XState testing) Advanced Topics? 40

Slide 41

Slide 41 text

1. PBT for End-to-End Testing (like this post) 2. Model-based testing (like XState testing) 3. PBT for Web Application (like Quickstrom) Advanced Topics? 41

Slide 42

Slide 42 text

Advanced Topics? 1. PBT for End-to-End Testing (like this post) 2. Model-based testing (like XState testing) 3. PBT for Web Application (like Quickstrom) 4. How to specify it! 42

Slide 43

Slide 43 text

Advanced Topics? 1. PBT for End-to-End Testing 2. Model-based testing 3. PBT for Web Application (like Quickstrom) 4. How to specify it! 43

Slide 44

Slide 44 text

Advanced Topics? 1. PBT for End-to-End Testing (like this post) 2. Model-based testing (like XState testing) 3. PBT for Web Application (like Quickstrom) 4. How to specify it! 5. ... and maybe some more I don’t know yet! 44

Slide 45

Slide 45 text

Test properties (invariants) with property-based testing and move example-based testing as document with `doctest`. Conclusion 45

Slide 46

Slide 46 text

Test properties (invariants) with property-based testing and move example-based testing as document with `doctest`. Let’s try to prove our programs by Property-Based Testing! Conclusion 46

Slide 47

Slide 47 text

Q & A 47