Slide 1

Slide 1 text

Property-based Testing with Hypothesis Michel Alexandre Salim • [email protected]

Slide 2

Slide 2 text

Overview Why Test? From Examples to Properties Taking a Deeper Look

Slide 3

Slide 3 text

Why Test?

Slide 4

Slide 4 text

We are not perfect Mistakes in ● Implementation ● Assumptions ● Third-party code! Test as Documentation ● Requirements are vague ● Tests provide specification ● Regression tests

Slide 5

Slide 5 text

From Examples to Properties

Slide 6

Slide 6 text

All Swans are White ... or are they?

Slide 7

Slide 7 text

Property-based Testing Example-Based ● Specific scenarios ● Must think of boundary conditions ● White box (mostly) - refactoring? ● Deterministic Property-Based ● Higher-order properties ● Can help find boundary conditions ● Black box ● Non-deterministic

Slide 8

Slide 8 text

More non-deterministic tests Property-Based Testing Test cases are randomly generated (within given boundaries). Try to find counter-examples to properties. Fuzz Testing Program inputs are randomly generated. Try and cause crash. Mutation Testing Program code is mutated. Verify that test suite kills mutants!.

Slide 9

Slide 9 text

Taking a Deeper Look

Slide 10

Slide 10 text

Hypothesis in Context History ● 1999: QuickCheck (Haskell) ● 2007: ScalaCheck ● 2013: Hypothesis ● 2015: Hypothesis 1.0

Slide 11

Slide 11 text

Generate Random test inputs generated (+ manual examples) Filter Only keep the data with the desired properties Test Run the test for each random data set Simplify On failure, shrink the failing inputs to find simplest failing case Report Report result of test runs

Slide 12

Slide 12 text

Because a live demonstration is worth a thousand words...

Slide 13

Slide 13 text

Online Resources Hypothesis Documentation https://hypothesis.readthedocs.org/en/latest/ pytest http://pytest.org/latest/ Examples from this presentation https://github.com/michel-slm/talk--property-based-testing--hypothesis/

Slide 14

Slide 14 text

Questions?