Zac Hatfield-Dodds


Zac Hatfield-Dodds


Zac’s modest goal is to help everyone write better code - mostly via bug-finding tools.

He spends his time working as a core dev on Hypothesis, Pytest, and other open-source projects; along with HypoFuzz and his PhD at the Australian National University. And if you can’t get to him via a computer, Zac can probably be found with a good book, a pile of chocolate, a long walk in the bush… or all three!


Introduction to Property-Based Testing


Has testing got you down? Ever spent a day writing tests, only to discover that you missed a bug because of some edge case you didn’t know about? Does it ever feel like writing tests is just a formality - that you already know your test cases will pass?

Property-based testing might be just what you need!

After this introduction to property-based testing, you’ll be comfortable with Hypothesis, a friendly but powerful property-based testing library. You’ll also known how to check and enforce robust properties in your code, and will have hands-on experience finding real bugs.

Where traditional example-based tests require you to write out each exact scenario to check - for example, assert divide(3, 4) == 0.75, property-based tests are generalised and assisted. You describe what kinds of inputs are allowed, write a test that should pass for any of them, and Hypothesis does the rest!

from hypothesis import given, strategies as st

@given(a=st.integers(), b=st.integers()) 
def test_divide(a, b): 
    result = a / b assert a == b * result
        

There’s the obvious ZeroDivisionError, fixable with b = st.integers().filter(lambda b: b != 0), but there’s another bug lurking. Can you see it? Hypothesis can!





Preparation


The tutorial is structured as three blocks, consisting of a short talk and extensive exercises for attendees.

  1. Property-Based Testing 101: core concepts and the core of the Hypothesis library
  2. Describe your Data: from numbers, to arrays, to recursive and more complicated things
  3. Common Tests: from “does not crash” to “write+read == noop” to ‘metamorphic relations’

The audience must be comfortable with decorators and traditional unit testing, with e.g. pytest.




...