clojure.test is the “official” Clojure test framework. It’s a simple
library, but one that is sufficient for many tasks.
There are other popular test frameworks for Clojure that provide more sophisticated semantics and capabilities. The most popular of these is Midje, available at https://github.com/marick/Midje.
is evaluates a
single expression, tests whether the result is logically true, and returns
is will report any
failures, including the actual values that were obtained, along with the
(optional) message provided with the assertion, if any:
(use 'clojure.test) (is (= 5 (+ 4 2)) "I never was very good at math...") ; FAIL in clojure.lang.PersistentList$EmptyList@1 (NO_SOURCE_FILE:1) ; I was never very good at math... ; expected: (= 5 (+ 4 2)) ; actual: (not (= 5 6)) ;= false (is (re-find #"foo" "foobar")) ;= "foo"
is defines a number of special
assertions that can be used within expressions. For example,
test that a certain type of error is thrown in the course of the
(is (thrown? ArithmeticException (/ 1 0))) ;= #<ArithmeticException java.lang.ArithmeticException: Divide by zero> (is (thrown? ArithmeticException (/ 1 1))) ; FAIL in clojure.lang.PersistentList$EmptyList@1 (NO_SOURCE_FILE:1) ; expected: (thrown? ArithmeticException (/ 1 1)) ; actual: nil ;= nil
is returns ...