When my project allows it, I like to use the attrs-map of functions to inline my tests. Seeing that not even @danielcompton seems to be aware of this feature, I thought I might tip about it here.

The thing is that the Clojure test runner looks for :test in the attrs-map of defined functions and runs them. Here’s a super simple example:

(defn abs "The absolute value of a number." {:test (fn [] (is (= (Math/abs 5) 5)) (is (= (Math/abs -2) 2)) (is (= (Math/abs 0) 0)))} [x] {:pre [(number? x)]} (if (pos? x) x (- x)))

I think this has lots of advantages. Keeping the (or at least, some) tests with the software under test makes for good documentation, promotes keeping the tests updated and is generally convenient.

With Clojure it gets extra powerful since you can use the REPL to grow the tests and the code you are testing without having to open some other file or window. I sometimes find myself entering a flow where I sort of mix REPL and test driven development. So instead of doing my experiments in the REPL window or even in a comment block I carry them out in that attrs-map , and then some of the experiments can just stay as tests.

It might not be for everyone, or for every occasion, but I recommend giving it a try. You might like it!

EDIT. In the original post I said this doesn’t work in ClojureScript. It does. See below. (I don’t know why it didn’t work when I tried this some while ago. It certainly wasn’t for lack of trying…)