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.

5665302502b3f48f4decb7f37bdeb348?s=128

Rae Knowler

June 16, 2019
Tweet

More Decks by Rae Knowler

Other Decks in Programming

Transcript

  1. Hypothesis Property-based testing for Python Rae Knowler PyCon Thailand 2019

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

    they/them/their https://www.flickr.com/photos/zurichtourism/5160475075
  3. What is property-based testing?

  4. @RaeKnowler #pyconth What is property-based testing? • Define properties rather

    than specific scenarios
  5. @RaeKnowler #pyconth What is property-based testing? • Define properties rather

    than specific scenarios • Give the test input and check that properties hold
  6. @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
  7. What is property-based testing?

  8. What is Hypothesis?

  9. @RaeKnowler #pyconth What is Hypothesis? • A property-based testing library

    for Python • Inspired by QuickCheck (Haskell) • https://hypothesis.works/ • https://hypothesis.readthedocs.io
  10. David MacIver and Sinister Katze @DRMacIver @sinister_katze

  11. @RaeKnowler #pyconth Hypothesis • Generates randomised test input

  12. @RaeKnowler #pyconth Hypothesis • Generates randomised test input • Shrinks

    examples to find the simplest failing input
  13. @RaeKnowler #pyconth Hypothesis • Generates randomised test input • Shrinks

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

    examples to find the simplest failing input • Remembers interesting inputs • Can deal with complex data types
  15. @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
  16. Getting started with Hypothesis

  17. @RaeKnowler #pyconth pip install hypothesis

  18. @RaeKnowler #pyconth A very simple example

  19. @RaeKnowler #pyconth A very simple example

  20. @RaeKnowler #pyconth A very simple example

  21. Slightly more advanced: Django integration

  22. @RaeKnowler #pyconth pip install hypothesis[django]

  23. @RaeKnowler #pyconth Django integration Screenshot of models here That might

    be too big so just the first one maybe?
  24. @RaeKnowler #pyconth Django integration

  25. @RaeKnowler #pyconth Django integration

  26. @RaeKnowler #pyconth Django integration

  27. @RaeKnowler #pyconth Django integration

  28. @RaeKnowler #pyconth Django integration

  29. Additional features

  30. @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
  31. @RaeKnowler #pyconth The Python scientific stack • Numpy strategies ◦

    arrays() • Pandas strategies ◦ pandas.Index, pandas.Series, pandas.DataFrame • Both of these are extras
  32. @RaeKnowler #pyconth Other extras • dpcontracts • lark • pytz

    • dateutil
  33. @RaeKnowler #pyconth Rule-based stateful testing • Give Hypothesis a set

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

  35. @RaeKnowler #pyconth Summary Use Hypothesis!

  36. @RaeKnowler #pyconth Summary Example repo: https://github.com/bellisk/hypothesis-django-example Slides:

  37. Thank you!