Introduction to Property-Based Testing

Zac Hatfield-Dodds (~Zac-HD)


Tests can be helpful: they can find bugs in new code, check for regressions in old code, and clarify precisely what the code is meant to do. But writing tests is tedious - and it's rare to think of an error when testing that you forgot when writing the code. My solution? Use tools that write tests for you!

In this tutorial, you'll learn the basic concepts of property-based testing, and how to apply them to find bugs real-world Python code using Hypothesis. We'll work through four blocks, each consisting of a short talk, live-coded demo, 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'
  4. Putting it into Practice: use what you've learned to find real bugs in a real project!

When we're done, you'll be able to apply Hypothesis with confidence - and find bugs in code from algorithms to business logic, whether you're a data scientist or web developer!


Attendees should be comfortable using decorators, and writing unit tests with pytest. Basic experience with parametrized tests and/or pytest fixtures is not required, but might be helpful.

Video URL:

Speaker Info:

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

He spends his time contributing to Hypothesis, Pytest, and other open-source projects; supporting the Python community as a PSF Fellow; and working on HypoFuzz and his PhD at the Australian National University. 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!

Speaker Links:

Section: Developer tools and automation
Type: Workshop
Target Audience: Advanced
Last Updated: