Clojure Tutorial: unit testing

By Xah Lee. Date:

This page is WORK IN PROGRESS.

is is the basic testing function.

the syntax is (is expr). expr must eval to true or true like? todo

clojure.test/is

(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) → Defines 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.

clojure.test/deftest

(deftest addition
  (is (= 4 (+ 2 2)))
  (is (= 7 (+ 3 4))))

clojure.test/testing

(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)))))

reference

If you have a question, put $5 at patreon and message me.

Clojure

  1. Clojure Install
  2. Leiningen Basics
  3. Emacs CIDER
  4. Doc Lookup, Find f

  1. Clojure Basics
  2. List, Map, …
  3. Functions
  4. Function Chaining
  5. Partial Function
  6. Binding Form
  7. regex
  8. Namespace
  9. metadata
  10. macro
  11. Calling Java
  12. Magic Chars '\@^#`~.

misc