Testing Clojure Code – Awesome “are”
test-is a great library for testing clojure. Thanks to Philippe, I recently discover “are” in test-is. This is most underrated/underused macro in test-is library.
Lets take an example, where I want to test a function given a date, it will return end of month.
Without are,
(deftest test-end-of-month
(is (= "2010-01-31 23:59" (end-of-month "2010-01-03 01:12")))
(is (= "2010-01-31 23:59" (end-of-month "2010-01-03 21:59")))
(is (= "2010-02-28 23:59" (end-of-month "2011-02-03 13:01")))
(is (= "2016-02-29 23:59" (end-of-month "2016-02-03 03:01")))
(is (= "2011-04-30 23:59" (end-of-month "2011-04-03 00:02"))))
With are,
(deftest test-end-of-month
(are [expected ts] (= expected (end-of-month ts))
"2010-01-31 23:59" "2010-01-03 01:12"
"2010-01-31 23:59" "2010-01-03 21:59"
"2011-02-28 23:59" "2011-02-03 13:01"
"2016-02-29 23:59" "2016-02-03 03:01"
"2011-04-30 23:59" "2011-04-03 00:02"))
The version without “are” has, so many duplications. It is difficult to read as it is cluttered. The version with “are” is easy to read, and it is so easy to add more test cases without much duplication.
Thanks Philippe for introducing me to wonders of “are”.

This is neat. But the documentation for ‘are’ says it may break error messages (line number reporting). Still worth it?
Shantanu Kumar
October 19, 2010 at 1:08 pm
Yah Shantanu, I think it is worth it.
In fact, when it fails, it shows exactly which case failed.
expected: (= "2016-02-29 23:59" (date-to-string (end-of-month (date-from-string "2016-01-03 03:01"))))actual: (not (= "2016-02-29 23:59" "2016-01-31 23:59"))
Siva Jagadeesan
October 19, 2010 at 2:00 pm