...

/

The Clojure Unit Test Library

The Clojure Unit Test Library

Take a deep dive into the clojure.test fundamental library, which we use to create unit tests in Clojure.

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:

Press + to interact
(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:

Press + to interact
(is (= 3 (+ 1 2)))

In ...

Access this course and 1400+ top-rated courses and projects.