Clojurians
# test-check

This page is not created by, affiliated with, or supported by Slack Technologies, Inc.

jmglov 14:48: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?

alexmiller 17:52:58

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

alexmiller 17:54:16

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

alexmiller 17:54:55

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

nwjsmith 18:24:06

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

nwjsmith 18:25: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

nwjsmith 18:25:35

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

nwjsmith 18:26: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.

nwjsmith 18:28: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

nwjsmith 18:29:44

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

nwjsmith 18:31: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)))

nwjsmith 18:33: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