The Hypothesis Testing Library for Python: An Introduction

Hypothesis is a Python library for creating tests which are simple to write and powerful when run, finding
cases in your code you wouldn’t have thought to look for. It is stable, powerful and easy to add to an existing test suite.

It works by letting you write tests that assert that something should be true for every case, not just the ones you happen to think of.

Think of a normal unit test as being something like the following:

  1. Set up some data.
  2. Perform some operations on the data.
  3. Assert something about the result.

Hypothesis lets you write tests which instead look like this:

  1. For all data matching some specification.
  2. Perform some operations on the data.
  3. Assert something about the result.

This is often called property-based testing, and was popularized by the Haskell library Quickcheck. [1]

I found out about the Hypothesis testing library about a year ago, started using it a few hours later, and have been using it ever since. A few months ago, I realized that I felt so strongly about the value and importance of the library that I should give a talk about it, and a few weeks ago that is just what I did. Here is my talk:

[1] https://hypothesis.readthedocs.io/en/latest/

 


Join the Red Hat Developer Program (it’s free) and get access to related cheat sheets, books, and product downloads.

Share