clojure.test

testの利用準備。

(use '[clojure.test])

キモはisマクロにあり。

   (is (= 4 (+ 2 2)))
   (is (instance? Integer 256))
   (is (.startsWith \"abcde\" \"ab\"))
   (is (thrown? ArithmeticException (/ 1 0))) 

isは第2引数をコメントとして受け取る。

(is (= 5 (+ 2 2)) \"Crazy arithmetic\")

testingマクロを使うと表明のグループにコメントできる。

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

ただし、testingはdeftestかwith-testの中でしか使えない。

with-testマクロでテストを定義する

   (with-test
       (defn my-function [x y]
         (+ x y))
     (is (= 4 (my-function 2 2)))
     (is (= 7 (my-function 3 4))))

deftestでもテストを定義できる

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

   (deftest subtraction
     (is (= 1 (- 4 3)))
     (is (= 3 (- 7 4))))