Fork me on GitHub
#test-check
<
2017-03-09
>
jmglov12:03:27

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?

Alex Miller (Clojure team)15:03:58

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

Alex Miller (Clojure team)15:03:16

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

Alex Miller (Clojure team)15:03:55

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

nwjsmith16:03:06

I’ve been working on a tool that can help with finding properties.

nwjsmith16:03:01

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

nwjsmith16:03:35

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

nwjsmith16:03:40

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.

nwjsmith16:03:25

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

nwjsmith16:03:44

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

nwjsmith16:03:09

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

nwjsmith16:03:13

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