Improving design and code quality with Property-based testing @ Techorama

Improving design and code quality with Property-based testing @ Techorama

A suite of traditional unit tests will only be as strong as the possible arguments or parameters tested against your code. Quoting Romeu Moura: If you take a String as an argument, then the works of Shakespeare in Japanese & Korean are ONE valid input. Obviously, this can be achieved with parameterized testing. However, this makes the unit tests so big that it is harder to understand which behaviour it is validating. We want our unit tests to also serve as living documentation so they should be comprehensible and to the point.

Enter Property-Based Testing. In Property-Based Testing, we randomly generate data points within the boundary of a property to verify the property’s behaviour. This not only lets us test edge cases that could expose unwanted and unexpected errors in the code but also enables us to make small tests that are readable and clear. Making these tests will also force you to think harder about the problem at hand and improve your design and code quality.

In this technical session, Kenny & João demonstrates and explains what property-based testing is, and how to implement it in C# with FsCheck and Java with JUnit-Quickcheck. If you are interested in improving your unit testing, so you don’t have to worry much about test data anymore, but more about the problem your code is solving, this talk is for you!

01abe62e641a626ed2dccc08ea2f8a14?s=128

Kenny Baas-Schwegler

October 03, 2018
Tweet

Transcript

  1. Improving design and code quality with Property-based testing

  2. Software Consultant - EventStormer Domain Driven Design Behaviour Driven Development

    Test Driven Development Continuous Delivery @kenny_baas baasie.com @joaoasrosa joaorosa.io
  3. None
  4. None
  5. None
  6. None
  7. None
  8. None
  9. None
  10. None
  11. None
  12. The greatest obstacle to discovery is not ignorance - it

    is the illusion of knowledge. - Daniel J. Boorstin
  13. None
  14. Confirmation Bias

  15. None
  16. None
  17. blue yellow yellow red green purple blue blue green purple

    yellow red green yellow purple blue red yellow green red blue purple yellow purple red blue purple blue green yellow red red purple green yellow blue blue yellow green red purple yellow blue green red purple yellow red blue green purple green red yellow purple blue purple green red blue yellow yellow red green purple blue blue green purple yellow red green yellow purple blue red yellow green red blue purple yellow purple red blue green red purple yellow blue green red purple yellow red blue green purple green red yellow purple blue purple green red blue yellow yellow red green purple blue blue green purple yellow red green yellow purple blue red yellow green red blue purple yellow purple red blue green red purple yellow blue green red purple yellow red blue green purple green red yellow purple blue purple green red blue yellow yellow
  18. To communicate effectively, the code must be based on the

    same language used to write the requirements - the same language that the developers speak with each other and with domain experts - Eric Evans
  19. None
  20. Dave Farley - Continuous Delivery Feedback Quickly Timely Reliable Customer

  21. Releasing software is too often an art; it should be

    an engineering discipline - Dave Farley
  22. None
  23. Single abstraction examples (aka Unit tests) • Learning from mistakes

    instead of only confirming • Fast feedback on new demands instead of slow changes • Living documentation on behaviour instead of on data
  24. None
  25. None
  26. None
  27. None
  28. Human CI • Always run tests locally before commiting •

    Once the code is committed, wait for the result of commited build ◦ Don’t go home, to lunch, to a meeting, to make coffee • If a teammate has gone home, lunch…. revert their changes! • Fix or revert within 10 minutes • When there are failures, Pair or Mob and learn together Dave Farley - Continuous Delivery
  29. Software Development is a learning process, working code is a

    side effect - Alberto Brandolini
  30. Benefits • Covers (eventually) the scope of all data points,

    forget about data • Shrinker looks for a small use case • Reproducible tests Next thing -> Fuzzing invalid inputs in System Tests
  31. Summary • (Also) Write tests with the intention of breaking

    code • Forget about data, focus on behaviour! • Quickly, Timely, Reliable Feedback • Human CI • Ohh… Have we mentioned Domain Driven Design?
  32. C# https://github.com/joaoasrosa/techorama-nl-property-based-testing Java https://gitlab.com/Baasie/Property-Based-Testing

  33. #CatTax