Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hypothesis: Property-based testing for Python

Hypothesis: Property-based testing for Python

We all know we should be writing tests, but coming up with tests for every edge case is hard work and you will inevitably miss some. In this talk, I'll show you how to use Hypothesis in your projects to find more bugs than you would imagine.

Rae Knowler

June 16, 2019
Tweet

More Decks by Rae Knowler

Other Decks in Programming

Transcript

  1. Who I am @RaeKnowler Python (Django, CKAN) PHP, Go, JavaScript

    they/them/their https://www.flickr.com/photos/zurichtourism/5160475075
  2. @RaeKnowler #pyconth What is property-based testing? • Define properties rather

    than specific scenarios • Give the test input and check that properties hold
  3. @RaeKnowler #pyconth What is property-based testing? • Define properties rather

    than specific scenarios • Give the test input and check that properties hold • Can automatically generate random inputs
  4. @RaeKnowler #pyconth What is Hypothesis? • A property-based testing library

    for Python • Inspired by QuickCheck (Haskell) • https://hypothesis.works/ • https://hypothesis.readthedocs.io
  5. @RaeKnowler #pyconth Hypothesis • Generates randomised test input • Shrinks

    examples to find the simplest failing input • Remembers interesting inputs
  6. @RaeKnowler #pyconth Hypothesis • Generates randomised test input • Shrinks

    examples to find the simplest failing input • Remembers interesting inputs • Can deal with complex data types
  7. @RaeKnowler #pyconth Hypothesis • Generates randomised test input • Shrinks

    examples to find the simplest failing input • Remembers interesting inputs • Can deal with complex data types • Works with pytest and unittest
  8. @RaeKnowler #pyconth Django integration • Hypothesis extra • Generate strategies

    for your Django models and forms • Examples obey validators • Generate child models ◦ e.g. a Company has Shops
  9. @RaeKnowler #pyconth The Python scientific stack • Numpy strategies ◦

    arrays() • Pandas strategies ◦ pandas.Index, pandas.Series, pandas.DataFrame • Both of these are extras
  10. @RaeKnowler #pyconth Rule-based stateful testing • Give Hypothesis a set

    of operations • It generates sequences of operations, looking for ones that cause failures