liveBook · Manning

Assertion language

As explained in Clojure: The Essential Reference

An assertion language is a set of constructs for expressing expected conditions and reporting whether test results satisfy them.

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

How Clojure: The Essential Reference covers it

How deep it goes
Covers assertion forms, diagnostics, grouping, exceptions, parameterization, and custom extension through assert-expr.
Examples
Uses Newton-method square-root tests, grouped corner cases, exception checks, repeated inputs, and a custom roughly assertion.
What it assumes
Assumes basic Clojure expressions and test definitions while demonstrating assertion behavior from basic expectations onward.
What you can do after
Apply it via clojure.test to write readable expectations and diagnose mismatches, exceptions, and approximate numeric results.
How it connects
Connects clojure.test with unit tests, testing contexts, fixtures, macros, and extensible assertion dispatch.

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.