A journey to Property-Based Testing
The purpose of this workshop is to understand by the practice an approach to testing that allows us to identify and check our business invariants. We will discover :
How it differs from traditional sampling approach (example-based testing)
Its advantages and limits
Source code
Source code is available here in :
Scala : Scalatest / Scalacheck
C# 10 : XUnit / FsCheck / Language-Ext / FluentAssertions
Connection - Calculator
Connect participants to the topic by making them write Unit Tests on the Calculator class :
Debriefing :
Concepts - PBT
Go through slides 4 to 15 to explain the different concepts :
Example-Based Testing vs PBT
What is PBT ?
Addition properties example
PBT by hands
Scalacheck / FsCheck / QuickCheck
Associated concepts
Demonstrate how to write properties on the Calculator, you can use the step-by-step guide to prepare yourself
Concrete Practice
Postal Parcel
Open `PostalParcel` :
Identify properties – 5’
What are the invariants ?
Write them in `PostalParcelPropertiesFlatSpec` with ScalaCheck / FsCheck - 15’
Bank withdrawal
Open `AccountService` :•Identify properties – 5’
What should we do to check those properties ?
Demonstrate how you could write properties on those knowing that Command and Account are tightly coupled as inputs :
Using properties + input filtering (when)
Using properties and Builder
How we would write tests on Bank withdrawal in real life (with Parameterized tests
Rental Calculator - PBT as a refactoring tool
Open `RentalCalculator`
Imagine this code is running in production and business is happy with it
Imagine you need to adapt it, but you need to be sure that you don’t introduce any regression
How PBT can help us ?
Let them code - 15'
Conclusion
When to apply PBT in real life
Associated anti-patterns
Reflect : Think about your last development, how this approach could have helped you ?
Resources
Scala exercises on PBT : https://www.scala-exercises.org/scalacheck/generators
Property based testing - step by step : https://www.leadingagile.com/2018/04/step-by-step-toward-property-based-testing/
Last updated