A small kata to explore and play with property-based testing
Published by Manuel Rivero on 11/04/2018
1. Introduction.
I’ve been reading Fred Hebert’s wonderful PropEr Testing online book about property-based testing. So to play with it a bit, I did a small exercise. This is its description:
1. 1. The kata.
We’ll implement a function that can tell if two sequences are equal regardless of the order of their elements. The elements can be of any type.
We’ll use property-based testing (PBT). Use the PBT library of your language (bring it already installed). Follow these constraints:
- You can’t use or compute frequencies of elements.
- Work test first: write a test, then write the code to make that test pass.
- If you get stuck, you can use example-based tests to drive the implementation on. However, at the end of the exercise, only property-based tests can remain.
Use mutation testing to check if you tests are good enough (we’ll do it manually injecting failures in the implementation code (by commenting or changing parts of it) and checking if the test are able to detect the failure to avoid using more libraries).
2. Driving a solution using both example-based and property-based tests.
I used Clojure and its test.check library (an implementation of QuickCheck) to do the exercise. I also used my favorite Clojure’s test framework: Brian Marick’s Midje which has a macro, forall, which makes it very easy to integrate property-based tests with Midje.
So I started to drive a solution using an example-based test (thanks to Clojure’s dynamic nature, I could use vectors of integers to write the tests.