test-check

jmglov 2017-03-09T12:48:27.498194Z

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?

alexmiller 2017-03-09T15:52:58.440024Z

@jmglov I like to say that generative testing is easy - the only hard parts are the generators and the properties

šŸ˜† 1
alexmiller 2017-03-09T15:54:16.458936Z

I took a stab at this topic of finding good properties in Clojure Applied, ch 8

alexmiller 2017-03-09T15:54:55.467652Z

I havenā€™t seen that link from @nwjsmith before but seems like generally good advice

nwjsmith 2017-03-09T16:24:06.877547Z

Iā€™ve been working on a tool that can help with finding properties.

nwjsmith 2017-03-09T16:25:01.890097Z

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

nwjsmith 2017-03-09T16:25:35.897953Z

Then it will run the mutant through the properties. If the mutant passes, then the tool reports an error.

nwjsmith 2017-03-09T16:26:40.913934Z

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.

nwjsmith 2017-03-09T16:28:25.939885Z

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

nwjsmith 2017-03-09T16:29:44.959453Z

This tool will generate a really simple mutant that passes all the properties, like (fn [x] (if (= x [1]) [2] (sort x)))

nwjsmith 2017-03-09T16:31:09.980511Z

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)))

nwjsmith 2017-03-09T16:33:13.011650Z

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

nwjsmith 2017-03-09T16:35:27.043232Z

(itā€™s based on https://github.com/rudymatela/fitspec)