Slide 1

Slide 1 text

Property-based testing With examples in C# @jovaneyck.bsky.social

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Replaying failed properties

Slide 8

Slide 8 text

Getting started

Slide 9

Slide 9 text

PBT pro == What input to generate + What properties to write + When (not) to PBT

Slide 10

Slide 10 text

Generating random inputs

Slide 11

Slide 11 text

Generating random inputs

Slide 12

Slide 12 text

Generating random inputs

Slide 13

Slide 13 text

Generating random inputs

Slide 14

Slide 14 text

Constraining generators

Slide 15

Slide 15 text

Combining generators

Slide 16

Slide 16 text

Combining generators

Slide 17

Slide 17 text

DnD generators

Slide 18

Slide 18 text

1d6?

Slide 19

Slide 19 text

1d6!

Slide 20

Slide 20 text

1d6 distribution

Slide 21

Slide 21 text

2d6?

Slide 22

Slide 22 text

!2d6

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

2d6!

Slide 25

Slide 25 text

2d6 v2

Slide 26

Slide 26 text

PBT pro == What input to generate + What properties to write + When (not) to PBT

Slide 27

Slide 27 text

Heuristics for “good properties”

Slide 28

Slide 28 text

generalizing unit tests John Hughes - Building on developers' intuitions (...) | Lambda Days 19

Slide 29

Slide 29 text

Generalizing unit tests

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Extracting domains from an email address

Slide 47

Slide 47 text

Start at the end

Slide 48

Slide 48 text

Serialization/deserialization

Slide 49

Slide 49 text

Serialization/deserialization

Slide 50

Slide 50 text

Inverse operations There and back again

Slide 51

Slide 51 text

Fuzzing You can throw anything at it

Slide 52

Slide 52 text

Commutativity Different paths, same destination https://fsharpforfunandprofit.com/posts/property-based-testing-2/

Slide 53

Slide 53 text

Invariants Some things never change https://fsharpforfunandprofit.com/posts/property-based-testing-2/

Slide 54

Slide 54 text

Idempotence The more things change, the more they stay the same https://fsharpforfunandprofit.com/posts/property-based-testing-2/

Slide 55

Slide 55 text

Let’s build Google

Slide 56

Slide 56 text

Let’s build Google

Slide 57

Slide 57 text

Let’s build Google

Slide 58

Slide 58 text

Metamorphic testing

Slide 59

Slide 59 text

Heuristics for good properties ● Generalized unit tests ● “Start at the end” ● Inverse operations: “There and back again” ● Fuzzing: “You can throw everything at it” ● Commutativity: “Different paths, same destination” ● Invariants: “Some things never change” ● Idempotence: “The more things change, the more they stay the same” ● Metamorphic properties

Slide 60

Slide 60 text

PBT pro == What input to generate + What properties to write + When (not) to PBT

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

Effort Confidence

Slide 63

Slide 63 text

When to PBT ● When high confidence is worth the effort ○ Critical code ● Problems with very large input/state spaces ○ Parsers ● Data structures, DDD aggregates/VO’s, monads ( ) ○ anything with clear and complete laws or invariants ● Fuzzy/non-deterministic problems ○ might not have deterministic or knowable input->output relationships ○ e.g. classifiers, LLM’s ● Legacy code ○ Writing properties will quickly (in)validate mental model of the SUT ● Stateful systems that can be modeled

Slide 64

Slide 64 text

When to PBT Mysteries of Dropbox, Property-based testing of a Distributed Synchronization Service, John Hughes et al.

Slide 65

Slide 65 text

When to PBT Delhaize rekent al maanden 'spookproducten' aan bij sommige klanten: "Ik moest betalen voor bak trappist die ik nooit kocht"

Slide 66

Slide 66 text

When (not) to PBT ● Test-Driven Development ○ It’s possible & fun BUT ○ Often even harder to do PBT in combination with TDD due to the friction between “universal properties’ and “incremental development”

Slide 67

Slide 67 text

Criteria Unit test Property Effort easy to write needs more cases you need to list them takes more effort to come up with good properties and generators One property covers lots (and lots) of cases Execution speed Fast 100/10.000x slower Bugs they catch depends on the test list edge cases, “+1 QA” in team Document the system examples business rules and invariants Keeping test code focused test data builders, custom assertions generators and properties Unit test vs. property-based test

Slide 68

Slide 68 text

Jessica Kerr’s thoughts on property-based testing “See how much thought goes into a property-based test? and this is a simple specification! I don’t recommend writing these for all your code – only the really important stuff.” “Property-based tests are best combined with example-based tests. Examples help you start organizing your thoughts, and they’re easier for future-you to read and understand when you come back to this code later. Humans think in examples. Programs don’t extrapolate. Property-based thinking and property-based testing can bridge between us and the computer. Math, it’s a tool.” https://jessitron.com/2013/04/25/property-based-testing-what-is-it/

Slide 69

Slide 69 text

OO tax

Slide 70

Slide 70 text

OO tax

Slide 71

Slide 71 text

PBT pro == What input to generate + What properties to write + When (not) to PBT

Slide 72

Slide 72 text

DIY 2

Slide 73

Slide 73 text

DIY 2 tests

Slide 74

Slide 74 text

DIY https://kata-log.rocks/mars-rover-kata https://bit.ly/pbt-workshop

Slide 75

Slide 75 text

Property-based testing With examples in C# @jovaneyck.bsky.social