Contribute Media
A thank you to everyone who makes this possible: Read More

Introduction to Property Based Testing

Translations: en en en

Description

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. On the other hand, writing tests is often tedious - and it's rare to think of an error when testing that you forgot when writing the code. Even worse, as scientists we write code because we don't yet know the correct answer - so how can we possibly test it? My favorite answer is to think about the higher-level properties of your system: even without the correct answer on hand, we can check that energy was conserved, reading gives us back the data we just wrote or converted, and so on.

Property-based testing encourages and rewards this style of thinking! Once you have a property or invariant in mind, it's easy to express that as a test - and then a library like Hypothesis will search for inputs that make your test fail. Instead of testing specific input-output pairs like 1 + 2 == 3, you might claim that a + b == b + a... and be informed that this is true for integers, but not for floats (not even finite floats!). This tutorial is for people comfortable with decorators and with reading, running, and writing Python unit tests.

It's structured as four blocks, each consisting of a short talk, live-coded demo, and extensive exercises: 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!

https://github.com/rsokl/testing-tutorial

Details

Improve this page