An assertion language expresses expected conditions in tests and reports whether actual results satisfy them. In Clojure, clojure.test extends basic assert with readable diagnostics, optional messages, grouped testing contexts, exception checks, and extensible assertions. section 22.9
Use its constructs to make expectations precise:
- Use is for truthy expressions and failure reports showing the expected form and actual result. section 22.9
- Group related checks with testing, including ordinary behavior and known corner cases. section 22.9
- Use are for applying one assertion pattern to several input-output pairs. section 22.9
- Check failures and messages with thrown? and thrown-with-msg?, or extend the language through assert-expr. section 22.9
A diagnostic assertion
(deftest sqrt-test (is (= 2 (sqrt 4)) "Expecting 2")) A failure reports the expectation and the actual value, exposing a floating-point result such as 2.000000000000002 rather than merely saying that assert failed. section 22.9
(require '[clojure.test :refer [is deftest test-var]]) (deftest sqrt-test (is (= 2 (sqrt 4)) "Expecting 2")) ; #1 (test-var #'sqrt-test) ; #2 ;; FAIL in () (form-init796879.clj:1) ; #3 ;; Expecting 2 ;; expected: (= 2 (sqrt 4)) ;; actual: (not (= 2 2.000000000000002))
Browse the compact Clojure example showing is, deftest, test-var, and a detailed failure report. section 22.9
