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

Improve your software quality with Property-Based Testing

Yoan
April 03, 2020

Improve your software quality with Property-Based Testing

Yoan

April 03, 2020
Tweet

More Decks by Yoan

Other Decks in Programming

Transcript

  1. @yot88
    PROPERTY-BASED TESTING
    (PBT)
    PBT

    View Slide

  2. @yot88
    A DEV STORY
    Scott Wlaschin
    https://fr.slideshare.net/ScottWlaschin/an-introduction-to-property-based-testing

    View Slide

  3. A dev story

    View Slide

  4. A dev story

    View Slide

  5. A dev story

    View Slide

  6. A dev story

    View Slide

  7. @yot88
    Which tests would you have been written ?

    View Slide

  8. A dev story

    View Slide

  9. A dev story

    View Slide

  10. A dev story

    View Slide

  11. A dev story

    View Slide

  12. A dev story

    View Slide

  13. A dev story

    View Slide

  14. A dev story

    View Slide

  15. A dev story
    What is our code
    coverage ?

    View Slide

  16. View Slide

  17. @yot88
    Let’s add new tests

    View Slide

  18. @yot88
    A dev story
    WTF ?

    View Slide

  19. @yot88
    A dev story

    View Slide

  20. A dev story

    View Slide

  21. @yot88
    A dev story

    View Slide

  22. @yot88
    A dev story
    Let’s rethink the approach
    Do not use specific examples anymore

    View Slide

  23. @yot88
    A dev story
    We can’t test the add by using +

    View Slide

  24. @yot88
    PBT to the rescue

    View Slide

  25. @yot88
    Testing with requirements / properties
    What about parameter order ? (compared to subtract)
    The parameter order does not matter
    when_I_add_two_numbers_the_result_should_not_depend_on_parameter_order
    “add 1” twice is the same as doing “add 2” (compared to multiply)
    What about adding 0 ?
    Adding 0 does nothing
    We are confident the implementation is correct if we test these properties
    It applies to all inputs

    View Slide

  26. Specification with properties
    Associativity property
    Commutativity property
    Identity property

    View Slide

  27. @yot88
    Is there a simpler way to do it ?

    View Slide

  28. @yot88
    What is Property based testing ?
    A property is the combination of an invariant with an input values generator.
    For each generated value, the invariant is treated as a predicate and checked whether it yields true or false for that value.
    As soon as there is one value which yields false, the property is said to be
    falsified, and checking is aborted.
    If a property cannot be invalidated after a specific amount of sample data, the
    property is assumed to be satisfied.

    View Slide

  29. What is Property based testing
    PBT

    View Slide

  30. @yot88
    What is Property based testing ?
    Describe the input
    Describe the properties of the output
    Have the computer try lots of random examples
    Check if it fails

    View Slide

  31. @yot88
    What is Property based testing

    View Slide

  32. @yot88
    What can we do in java ?

    View Slide

  33. Junit-quickcheck
    https://pholser.github.io/junit-quickcheck/site/0.9.1/

    View Slide

  34. Junit-quickcheck

    View Slide

  35. Junit-quickcheck

    View Slide

  36. Junit-quickcheck

    View Slide

  37. Junit-quickcheck

    View Slide

  38. Junit-quickcheck

    View Slide

  39. Junit-quickcheck

    View Slide

  40. Junit-quickcheck

    View Slide

  41. Junit-quickcheck

    View Slide

  42. Junit-quickcheck

    View Slide

  43. @yot88
    And in real life ?

    View Slide

  44. Real life

    View Slide

  45. Real life – identify properties
    The balance should be decremented of the withdrawal amount when
    I have enough money
    Or the overdraft is authorized for my account
    I must not be allowed to withdraw when
    The withdraw amount is over my max withdrawal amount
    My balance is insufficient, and overdraft is not authorized for my account

    View Slide

  46. Real life – custom generators
    To use them
    Or use the serviceloader by creating a file called:
    com.pholser.junit.quickcheck.generator.Generator in
    META-INF/services

    View Slide

  47. Real life – implementation

    View Slide

  48. @yot88
    Vavr stuff ?

    View Slide

  49. PBT in vavr

    View Slide

  50. PBT in vavr

    View Slide

  51. @yot88
    And so ?

    View Slide

  52. @yot88
    PBT and example-based tests
    PBTs are more general
    1 property-based test can replace many example-based tests
    PBTs can reveal edge cases
    Nulls, negative numbers, weird strings (encoding for example), …
    PBTs ensures deep understanding of the business invariants
    Example-based tests are still helpful though
    Easier to understand & start with

    View Slide

  53. @yot88
    What could we do together ?
    NEXT STEPS

    View Slide

  54. @yot88
    Resources
    An introduction to property-based testing – Scott Wlaschin
    Property-Based Testing for everyone - Romeu Moura (Video)
    Property-based testing in Java with JUnit-Quickcheck - Kenny Baas-Schwegler
    https://baasie.com/2017/06/12/property-based-testing-in-java-with-junit-quickcheck-part-2-
    generators/
    Property Testing with vavr
    junit-quickcheck documentation

    View Slide