In this session, we're going to discover Property-Based Testing.
Let's redo some kata we've already tried and see how PBT can change the way we approach the problem.
We're going to use Eris as our PBT tool. It will generate multiple random values to try falsifying our properties. In case of a property being falsified it will shrink the values to try to get a simpler example.
Here are some ideas but feel free to try your own ones :
- Repeating a string twice give a string twice as long as the original one
- Reversing a string twice gives the same string
- A sorted list is the same length of the original list
- Monday + 7 days is a Monday
Try seeing the generated values (with a debugger or var_dump
) and create a bug just to see the shrinking in action. You can play with the view_shriking
test.
https://www.codurance.com/katalyst/password-validation
https://github.com/ardalis/kata-catalog/blob/main/katas/FizzBuzz.md
Imagine some examples with what you know with the business rules your work on every day.
If you want to learn more about Property-Based Testing you can find resources here.
Run composer install
to get dependencies.
Alternatively, you can install dependencies using docker with docker-compose run --rm php composer install
.
You can run tests with PhpUnit using ./vendor/bin/phpunit
.
If you prefer using docker you can run tests with docker-compose run --rm php ./vendor/bin/phpunit
.