Property-based testing: work harder, not smarter

Property-based testing: work harder, not smarter

Testing is an important part of software development, but how many tests is enough? Should you write a test for every possible value that could be submitted to a function? Property-based testing is a technique that can help you with this conundrum. Instead of writing a specific set of tests, write some generalized tests and let a tool randomly test inputs against your code.

I'll explain in more detail what property-based testing is, how it works, and show some practical examples using Eris, a new PHPUnit extension modeled after Haskell's QuickCheck library.

2fe1b2866cfd498a513295d60d5cad0f?s=128

Joel Clermont

November 07, 2015
Tweet

Transcript

  1. Property-based Testing
 Work smarter, not harder Joel Clermont
 @jclermont

  2. What to expect • Why automated testing?

  3. What to expect • Why automated testing? • Define property-based

    testing
  4. What to expect • Why automated testing? • Define property-based

    testing • Anatomy of a test
  5. What to expect • Why automated testing? • Define property-based

    testing • Anatomy of a test • Examples
  6. What to expect • Why automated testing? • Define property-based

    testing • Anatomy of a test • Examples • Next steps
  7. Why automated testing?

  8. Automated testing • We already test manually • Developers like

    to save time • Make the computer work for you • Bonus: Code design improves with unit testing
  9. Questions?

  10. What is 
 property-based testing?

  11. Unit test • Specify inputs • Run code under test

    • Make assertions on output
  12. How many tests 
 are enough?

  13. Property-based test • Define a way to generate inputs •

    Run code under test • Make assertions about output • This is the tricky part!
  14. How can I assert anything about the output without knowing

    the input?
  15. A property is something your system exhibits for any input

  16. Sample properties • x + 0 = x • x

    * 1 = x • x + y = y + x • strlen(strpad(str, n)) === n
  17. Different paths, same destination http://fsharpforfunandprofit.com/posts/property-based-testing-2/

  18. There and back again http://fsharpforfunandprofit.com/posts/property-based-testing-2/

  19. Some things never change http://fsharpforfunandprofit.com/posts/property-based-testing-2/

  20. The more things change, the more they stay the same

    http://fsharpforfunandprofit.com/posts/property-based-testing-2/
  21. Hard to prove, easy to verify http://fsharpforfunandprofit.com/posts/property-based-testing-2/

  22. Two heads are better than one http://fsharpforfunandprofit.com/posts/property-based-testing-2/

  23. Danger!! Don’t reimplement your function in your test

  24. Questions?

  25. Eris PHPUnit extension for property-based testing

  26. Anatomy of a test

  27. Generator

  28. Filter

  29. Assertions

  30. Generator demo

  31. Questions?

  32. Examples

  33. DEMO

  34. Questions?

  35. Next steps • http://fsharpforfunandprofit.com/posts/property- based-testing/ • http://fsharpforfunandprofit.com/posts/property- based-testing-2/ • midwest.io

    - Jessica Kerr 
 https://www.youtube.com/watch?v=shngiiBfD80 • https://github.com/giorgiosironi/eris
  36. Questions? @jclermont

  37. Thank you! @jclermont