Unit Tests

Many schools of thought exist on how, what, and when to test, and it’s a sensitive subject for many people. As such, we’ll simply give an overview of the basic tools available for testing and leave it up to you to decide how and when to use them.

The Test API

Clojure provides built-in support for testing via the clojure.test namespace. When a new project is created, a test package is generated along with it.

Let’s take a quick look at what the clojure.test API looks like and how to work with it. The simplest way to write tests is to create assertions using the is macro. The following are a few examples of how it works:

 user> (​require​ '[clojure.test :refer [is]])
 nil
 user> (​is​ (= 4 (+ 2 2)))
 true
 user> (​is​ (= 5 (+ 2 2))) ...

Get Web Development with Clojure, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.