Unit Testing

In Clojure the unit under test is the function. Unit test coverage should test all public function that form the API of their respective namespace.

clojure.test namespace provides a unit testing framework and is included in the Clojure library, so is available in all Clojure projects.

Principles for writing test code

  • One test namespace for each src namespace
  • One deftest function for each function under test
  • Multiple is assertions for one function
  • Group assertions in testing and provide a meaningful description of that grouping, adding more information when reviewing test failures especially for larger code bases.

Requiring Namespaces

A test namespace has a singular purpose to test a matching application namespace. Therefore the idiomatic approach is to :refer specific functions from clojure.test.

(require '[clojure.test :refer [deftest is testing]])

The namespace under test should be referred, typically using the alias SUT for software under test.

(require '[practicalli.playground :as SUT])

Add clojure.test to the namespace definition along with the namespace under test.

(ns practicalli.app-namespace-test
  (:require '[clojure.test :refer [deftest is testing]]
             [practicalli.app-namespace :as SUT]))

SUT alias - software under test

The alias SUT, meaning software under test, is a common convention in unit testing. Using the SUT alias makes it easier for developers to see which functions from the application are being tested at a glance.

Project structure with tests

By convention, separate src and test directories are used to hold the source code and the code that tests the source code.

For each source code file in src there should be a corresponding file in test with the same name and -test postfix.

For example, code to test the src/codewars/rock_paper_scissors.clj is saved in the file src/codewars/rock_paper_scissors_test.clj file.

Clojure project structure - src and test branches

Create Projects from templates

Templates typically include a parallel test and src directory structure. The clj-new tool has build it templates (app, lib) and will create src and test directories in the projects it creates.

clojure -M:project/new app practicalli/rock-paper-scissors-lizard-spock


results matching ""

    No results matching ""