Search⌘ K
AI Features

The Clojure Unit Test Library

Explore how to effectively use the Clojure core unit testing library clojure.test. Understand how to write assertions, handle expected errors, define tests with macros like deftest and with-test, organize tests for clarity, and run them interactively using the REPL. Gain the skills to validate smaller code units, mainly functions, ensuring robust and reliable Clojure code.

Unit testing in Clojure

Unit testing is about testing the smallest unit of code. In functional languages, the unit is typically the function, which is true in Clojure.

In Clojure, we have a few frameworks that can help us do unit testing. However, because we’re here to explore the core functionalities, we’re only going to talk about clojure.test, which is one of the fundamental core libraries and the one that will ensure we do a really good job in testing.

The clojure.test library

This is a unit testing framework available in Clojure that can be imported into the desired namespace, like this:

Clojure
(ns main
(:require [clojure.test :as t]))

Assertions

As in any other unit test, one of the main characteristics of a framework is its way of doing assertions, which are comparisons of expected results to the actual results. In the clojure.test library, this is the is macro, which receives the conditional validation to assert. Let’s see a few examples:

Clojure
(is (= 3 (+ 1 2)))

In the previous example, ...