Staying Out of Trouble

So which is better, traditional unit tests or generative tests? The answer is simple: yes. Both traditional tests and their generative cousins have strengths and weaknesses. And both have a place in making sure your code is doing what it is supposed to be doing.

Traditional unit tests do have one huge advantage: they are shatteringly obvious. Does it work when I do this? Yes. Does it work when I do that? Yes. OK, then we’re good. So, if we were trying to test this simple function:

 (​defn​ f [a b] (/ a b))

we might write this test:

 (deftest test-f
  (is (= 1/2 (f 1 2)))
  (is (= 1/2 (f 3 6)))
  (is (= 1 (f 10 10))))

And we know that for these specific instances, it works. The drawback of this kind ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.