$30 off During Our Annual Pro Sale. View Details »

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.

Joel Clermont

November 07, 2015
Tweet

More Decks by Joel Clermont

Other Decks in Programming

Transcript

  1. Property-based Testing

    Work smarter, not harder
    Joel Clermont

    @jclermont

    View Slide

  2. What to expect
    • Why automated testing?

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  7. Why automated testing?

    View Slide

  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

    View Slide

  9. Questions?

    View Slide

  10. What is 

    property-based testing?

    View Slide

  11. Unit test
    • Specify inputs
    • Run code under test
    • Make assertions on output

    View Slide

  12. How many tests 

    are enough?

    View Slide

  13. Property-based test
    • Define a way to generate inputs
    • Run code under test
    • Make assertions about output
    • This is the tricky part!

    View Slide

  14. How can I assert anything
    about the output without
    knowing the input?

    View Slide

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

    View Slide

  16. Sample properties
    • x + 0 = x
    • x * 1 = x
    • x + y = y + x
    • strlen(strpad(str, n)) === n

    View Slide

  17. Different paths, same destination
    http://fsharpforfunandprofit.com/posts/property-based-testing-2/

    View Slide

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

    View Slide

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

    View Slide

  20. The more things change, the more they stay the same
    http://fsharpforfunandprofit.com/posts/property-based-testing-2/

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  24. Questions?

    View Slide

  25. Eris
    PHPUnit extension for property-based testing

    View Slide

  26. Anatomy of a test

    View Slide

  27. Generator

    View Slide

  28. Filter

    View Slide

  29. Assertions

    View Slide

  30. Generator demo

    View Slide

  31. Questions?

    View Slide

  32. Examples

    View Slide

  33. DEMO

    View Slide

  34. Questions?

    View Slide

  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

    View Slide

  36. Questions?
    @jclermont

    View Slide

  37. Thank you!
    @jclermont


    View Slide