I'm sorry if I'm repeating an oft-asked and answered question, but I'm really struggling with how to write properties to test my functionality without re-implementing the code I'm testing. Does anyone have a solid resource with some nice examples that could lead me out of the wilderness here?
@jmglov https://fsharpforfunandprofit.com/posts/property-based-testing-2/
@jmglov I like to say that generative testing is easy - the only hard parts are the generators and the properties
I took a stab at this topic of finding good properties in Clojure Applied, ch 8
I havenāt seen that link from @nwjsmith before but seems like generally good advice
Iāve been working on a tool that can help with finding properties.
It will take a set of properties, and the arg and return generators (e.g. from specās fdef
), and generate a mutation of the function under test
Then it will run the mutant through the properties. If the mutant passes, then the tool reports an error.
It presents the mutant, which is usually very simple, and that can guide you to your next property. You play āwhack-a-mutantā until no mutants pass.
An example: letās say weāre writing properties for sort
, you might start with the property that the input and output lists have the same count
This tool will generate a really simple mutant that passes all the properties, like (fn [x] (if (= x [1]) [2] (sort x)))
We want to whack this mutant (which isnāt our sort
function). So we write a property that the input and output lists have to have the same elements. Our tool finds a new mutant that passes our properties (fn [x] (if (= x [1 2]) [2 1] (sort x)))
Finally, we write a property that says the elements of the return list should be in ascending order, and the tool reports no new mutants. This means weāve written a complete set of properties
(itās based on https://github.com/rudymatela/fitspec)