Clojure Tutorial: unit testing

By Xah Lee. Date:

This page is WORK IN PROGRESS.

tested on clojure 1.8

is is the basic testing function.

the syntax is (is expr).

expr must eval to true or true like? xtodo

(clojure.test/is (= 4 (+ 2 2)))
(clojure.test/is
 (= 4 (+ 2 2))
 "test addition"
 )
(clojure.test/is (= 4 (+ 2 2)))

(clojure.test/is (instance? Integer 256))

(clojure.test/is (.startsWith "abcde" "ab"))

deftest

(deftest name body)
Define a test function with no arguments. Test functions may call other tests, so tests may be composed. If you compose tests, you should also define a function named test-ns-hook; run-tests will call test-ns-hook instead of testing all vars.

Note: Actually, the test body goes in the :test metadata on the var, and the real function (the value of the var) calls test-var on itself.

When *load-tests* is false, deftest is ignored.

(deftest addition
  (is (= 4 (+ 2 2)))
  (is (= 7 (+ 3 4))))
(testing "with negative integers"
    (is (= -4 (+ -2 -2)))
    (is (= -1 (+ 3 -4))))

can be nested

(testing "Arithmetic"
  (testing "with positive integers"
    (is (= 4 (+ 2 2)))
    (is (= 7 (+ 3 4))))
  (testing "with negative integers"
    (is (= -4 (+ -2 -2)))
    (is (= -1 (+ 3 -4)))))