O'Reilly logo

Clojure Programming by Brian Carper, Christophe Grand, Chas Emerick

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

clojure.test

clojure.test is the “official” Clojure test framework. It’s a simple library, but one that is sufficient for many tasks.

Note

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.

Assertions in clojure.test use the is macro. is evaluates a single expression, tests whether the result is logically true, and returns that value. 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.[359] For example, thrown? will test that a certain type of error is thrown in the course of the expression’s evaluation:

(is (thrown? ArithmeticException (/ 1 0)))                         1
;= #<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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required